[PATCH] D133361: [BPF] Attribute preserve_static_offset for structs

2023-10-30 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

I tried a couple of examples with both preserve_access_index and 
preserve_static_offset (both '__preserve_access_index __preserve_static_offset' 
and '__preserve_static_offset __preserve_access_index'). Looks they cooperate 
with each other properly. __preserve_static_offset is for base pointer while
__preserve_access_index is for offset. So the current implementation seems 
OKAY. But __preserve_static_offset actually means 'static offset' (no offset 
change),
so __preserve_access_index seems not necessary, so if possible maybe clang 
frontend should make a choice to remove __preserve_access_index if
__preserve_static_offset is there. But this can be done later (need frontend 
review again, need to update doc etc.)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133361/new/

https://reviews.llvm.org/D133361

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D133361: [BPF] Attribute preserve_static_offset for structs

2023-10-30 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

I went through LLVM part of the change, mostly look good to me from high-level 
and I need to go through another pass with details.
It would be great if you can post corresponding kernel patches which utilizes 
this functionality?




Comment at: clang/include/clang/Basic/AttrDocs.td:2227
+  void buz(struct bar *g) {
+g->a.b = 42;
+  }

g->b.a ?



Comment at: llvm/lib/Target/BPF/BPFPreserveStaticOffset.cpp:82
+//   for (int i = 0; i < 4; ++i)
+// foo(ctx->x[i]);
+//

Is it possible that such a pattern already changed by GVN/CSE so later on 
converting to preserve_static_offset becomes impossible? I guess it is unlikely 
but want to double check. Maybe add a comment to clarify.



Comment at: llvm/lib/Target/BPF/BPFPreserveStaticOffset.cpp:102
+// - the (get.and.load %p) would come from the callee function.
+// Thus clobbering CSE / GVN passes done after inlining.
+

So the above implies that this is bad for CSE/GVN due to different 
representation, right? But we want to do (1) since we want to avoid later 
CSE/GVN etc, so argument of clobbering is not that important, right?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133361/new/

https://reviews.llvm.org/D133361

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D133361: [BPF] Attribute btf_decl_tag("ctx") for structs

2023-08-29 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

> I can rename these things, but tbh I don't think this functionality would be 
> useful anywhere outside BPF, thus such renaming would be kind-of deceptive 
> (and in case it would be useful, the renaming could be done at the time of 
> second use).

Then let us keep this for now. We can decide what to do after clang frontend 
people reviewed this code.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133361/new/

https://reviews.llvm.org/D133361

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D133361: [BPF] Attribute btf_decl_tag("ctx") for structs

2023-08-28 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

I will try to review other parts of code in the next few days.




Comment at: llvm/include/llvm/IR/Intrinsics.td:2432
   ImmArg>]>;
+def int_context_marker_bpf : DefaultAttrsIntrinsic<[llvm_ptr_ty],
+   [llvm_ptr_ty],

eddyz87 wrote:
> yonghong-song wrote:
> > Is it possible to make this builtin as BPF specific one?
> Currently `llvm::Intrinsic::context_marker_bpf` gets defined in 
> `llvm/IR/Intrinsics.h` (via include of generated file `IntrinsicEnums.inc`, 
> same as `preserve_{struct,union,array}_access_index`).
> 
> BPF specific intrinsics are defined in `llvm/IR/IntrinsicsBPF.h` (generated 
> directly w/o .inc intermediary).
> 
> Thus, if I move `context_marker_bpf` to `IntrinsicsBPF.td` I would have to 
> include `IntrinsicsBPF.h` in `CGExpr.cpp`. However, I don't see any target 
> specific includes in that file.
> 
I went through the related clang code and indeed found it is hard to get a BPF 
target defined function in CGF or CGM. On the other hand, we can consider this 
new builtin under the umbrella "Intrinsics that are used to preserve debug 
information".  Maybe we can rename the intrinsic name
to 'int_preserve_context_marker'? The goal of this builtin to preserve certain 
load/store which should be immune from optimizations. I try to generalize this 
so your function name 'wrapWithBPFContextMarker' can be renamed to
'wrapWithContextMarker'. There is no need to mention BPF any more.

In the commit message, you can mention that
similar to int_preserve_array_access_index, int_preserve_context_marker is only 
implemented in BPF backend. But other architectures can implement processing 
these intrinsics if they want to achieve some results similar to bpf backend.

WDYT? 


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133361/new/

https://reviews.llvm.org/D133361

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D133361: [BPF] Attribute btf_decl_tag("ctx") for structs

2023-08-27 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added inline comments.



Comment at: llvm/include/llvm/IR/Intrinsics.td:2432
   ImmArg>]>;
+def int_context_marker_bpf : DefaultAttrsIntrinsic<[llvm_ptr_ty],
+   [llvm_ptr_ty],

Is it possible to make this builtin as BPF specific one?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133361/new/

https://reviews.llvm.org/D133361

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156726: Make globals with mutable members non-constant, even in custom sections

2023-08-17 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

> Ah, right right - sorry, failed to push. Here it is: 
> 2993243c45abdb4f2bc3979336d054be165b1134 
> 

Thanks! I tested and the problem with bpf backend is resolved!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156726/new/

https://reviews.llvm.org/D156726

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156726: Make globals with mutable members non-constant, even in custom sections

2023-08-16 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

In D156726#4593671 , @dblaikie wrote:

> Fixed in 19c216b5ab7c34c297b4c3c4da64b49b3c830a3a

Hi, @dblaikie I cannot find the above commit in llvm-project and with latest 
llvm-project 'main' branch, the problem seems still exist.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156726/new/

https://reviews.llvm.org/D156726

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D144829: [BPF] Add a few new insns under cpu=v4

2023-07-26 Thread Yonghong Song via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG6c412b6c6faa: [BPF] Add a few new insns under cpu=v4 
(authored by yonghong-song).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144829/new/

https://reviews.llvm.org/D144829

Files:
  clang/lib/Basic/Targets/BPF.cpp
  clang/lib/Basic/Targets/BPF.h
  clang/test/Misc/target-invalid-cpu-note.c
  llvm/lib/Target/BPF/AsmParser/BPFAsmParser.cpp
  llvm/lib/Target/BPF/BPF.td
  llvm/lib/Target/BPF/BPFISelDAGToDAG.cpp
  llvm/lib/Target/BPF/BPFISelLowering.cpp
  llvm/lib/Target/BPF/BPFISelLowering.h
  llvm/lib/Target/BPF/BPFInstrFormats.td
  llvm/lib/Target/BPF/BPFInstrInfo.td
  llvm/lib/Target/BPF/BPFMIPeephole.cpp
  llvm/lib/Target/BPF/BPFMISimplifyPatchable.cpp
  llvm/lib/Target/BPF/BPFSubtarget.cpp
  llvm/lib/Target/BPF/BPFSubtarget.h
  llvm/lib/Target/BPF/Disassembler/BPFDisassembler.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCFixups.h
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp
  llvm/test/CodeGen/BPF/assembler-disassembler-v4.s
  llvm/test/CodeGen/BPF/bswap.ll
  llvm/test/CodeGen/BPF/gotol.ll
  llvm/test/CodeGen/BPF/ldsx.ll
  llvm/test/CodeGen/BPF/movsx.ll
  llvm/test/CodeGen/BPF/sdiv_smod.ll

Index: llvm/test/CodeGen/BPF/sdiv_smod.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/sdiv_smod.ll
@@ -0,0 +1,44 @@
+; RUN: llc -march=bpfel -mcpu=v4 -verify-machineinstrs -show-mc-encoding < %s | FileCheck %s
+; Source:
+;  int foo(int a, int b, int c) {
+;return a/b + a%c;
+;  }
+;  long bar(long a, long b, long c) {
+;   return a/b + a%c;
+; }
+; Compilation flags:
+;   clang -target bpf -O2 -S -emit-llvm t.c
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
+define dso_local i32 @foo(i32 noundef %a, i32 noundef %b, i32 noundef %c) local_unnamed_addr #0 {
+entry:
+  %div = sdiv i32 %a, %b
+  %rem = srem i32 %a, %c
+  %add = add nsw i32 %rem, %div
+  ret i32 %add
+}
+
+; CHECK:   w0 = w1
+; CHECK-NEXT:  w1 s/= w2   # encoding: [0x3c,0x21,0x01,0x00,0x00,0x00,0x00,0x00]
+; CHECK-NEXT:  w0 s%= w3   # encoding: [0x9c,0x30,0x01,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
+define dso_local i64 @bar(i64 noundef %a, i64 noundef %b, i64 noundef %c) local_unnamed_addr #0 {
+entry:
+  %div = sdiv i64 %a, %b
+  %rem = srem i64 %a, %c
+  %add = add nsw i64 %rem, %div
+  ret i64 %add
+}
+; CHECK:   r0 = r1
+; CHECK-NEXT:  r1 s/= r2   # encoding: [0x3f,0x21,0x01,0x00,0x00,0x00,0x00,0x00]
+; CHECK-NEXT:  r0 s%= r3   # encoding: [0x9f,0x30,0x01,0x00,0x00,0x00,0x00,0x00]
+
+attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+
+!llvm.module.flags = !{!0, !1}
+!llvm.ident = !{!2}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 7, !"frame-pointer", i32 2}
+!2 = !{!"clang version 17.0.0 (https://github.com/llvm/llvm-project.git c102025a4299e74767cdb4dfba8abbf6cbad820b)"}
Index: llvm/test/CodeGen/BPF/movsx.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/movsx.ll
@@ -0,0 +1,86 @@
+; RUN: llc -march=bpfel -mcpu=v4 -verify-machineinstrs -show-mc-encoding < %s | FileCheck %s
+; Source:
+;   short f1(int a) {
+; return (char)a;
+;   }
+;   int f2(int a) {
+; return (short)a;
+;   }
+;   long f3(int a) {
+; return (char)a;
+;   }
+;   long f4(int a) {
+; return (short)a;
+;   }
+;   long f5(int a) {
+; return a;
+;   }
+;   long f6(long a) {
+; return (int)a;
+;   }
+; Compilation flags:
+;   clang -target bpf -O2 -mcpu=v4 -S -emit-llvm t.c
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
+define dso_local i16 @f1(i32 noundef %a) local_unnamed_addr #0 {
+entry:
+  %conv = trunc i32 %a to i8
+  %conv1 = sext i8 %conv to i16
+  ret i16 %conv1
+}
+; CHECK:  w0 = (s8)w1 # encoding: [0xbc,0x10,0x08,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
+define dso_local i32 @f2(i32 noundef %a) local_unnamed_addr #0 {
+entry:
+  %sext = shl i32 %a, 16
+  %conv1 = ashr exact i32 %sext, 16
+  ret i32 %conv1
+}
+; CHECK:  w0 = (s16)w1# encoding: [0xbc,0x10,0x10,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
+define dso_local i64 @f3(i32 noundef %a) 

[PATCH] D144829: [BPF] Add a few new insns under cpu=v4

2023-07-25 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 544055.
yonghong-song added a comment.

- Add more tests in assembler-disassembler-v4.s and gotol.ll.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144829/new/

https://reviews.llvm.org/D144829

Files:
  clang/lib/Basic/Targets/BPF.cpp
  clang/lib/Basic/Targets/BPF.h
  clang/test/Misc/target-invalid-cpu-note.c
  llvm/lib/Target/BPF/AsmParser/BPFAsmParser.cpp
  llvm/lib/Target/BPF/BPF.td
  llvm/lib/Target/BPF/BPFISelDAGToDAG.cpp
  llvm/lib/Target/BPF/BPFISelLowering.cpp
  llvm/lib/Target/BPF/BPFISelLowering.h
  llvm/lib/Target/BPF/BPFInstrFormats.td
  llvm/lib/Target/BPF/BPFInstrInfo.td
  llvm/lib/Target/BPF/BPFMIPeephole.cpp
  llvm/lib/Target/BPF/BPFMISimplifyPatchable.cpp
  llvm/lib/Target/BPF/BPFSubtarget.cpp
  llvm/lib/Target/BPF/BPFSubtarget.h
  llvm/lib/Target/BPF/Disassembler/BPFDisassembler.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCFixups.h
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp
  llvm/test/CodeGen/BPF/assembler-disassembler-v4.s
  llvm/test/CodeGen/BPF/bswap.ll
  llvm/test/CodeGen/BPF/gotol.ll
  llvm/test/CodeGen/BPF/ldsx.ll
  llvm/test/CodeGen/BPF/movsx.ll
  llvm/test/CodeGen/BPF/sdiv_smod.ll

Index: llvm/test/CodeGen/BPF/sdiv_smod.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/sdiv_smod.ll
@@ -0,0 +1,44 @@
+; RUN: llc -march=bpfel -mcpu=v4 -verify-machineinstrs -show-mc-encoding < %s | FileCheck %s
+; Source:
+;  int foo(int a, int b, int c) {
+;return a/b + a%c;
+;  }
+;  long bar(long a, long b, long c) {
+;   return a/b + a%c;
+; }
+; Compilation flags:
+;   clang -target bpf -O2 -S -emit-llvm t.c
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
+define dso_local i32 @foo(i32 noundef %a, i32 noundef %b, i32 noundef %c) local_unnamed_addr #0 {
+entry:
+  %div = sdiv i32 %a, %b
+  %rem = srem i32 %a, %c
+  %add = add nsw i32 %rem, %div
+  ret i32 %add
+}
+
+; CHECK:   w0 = w1
+; CHECK-NEXT:  w1 s/= w2   # encoding: [0x3c,0x21,0x01,0x00,0x00,0x00,0x00,0x00]
+; CHECK-NEXT:  w0 s%= w3   # encoding: [0x9c,0x30,0x01,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
+define dso_local i64 @bar(i64 noundef %a, i64 noundef %b, i64 noundef %c) local_unnamed_addr #0 {
+entry:
+  %div = sdiv i64 %a, %b
+  %rem = srem i64 %a, %c
+  %add = add nsw i64 %rem, %div
+  ret i64 %add
+}
+; CHECK:   r0 = r1
+; CHECK-NEXT:  r1 s/= r2   # encoding: [0x3f,0x21,0x01,0x00,0x00,0x00,0x00,0x00]
+; CHECK-NEXT:  r0 s%= r3   # encoding: [0x9f,0x30,0x01,0x00,0x00,0x00,0x00,0x00]
+
+attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+
+!llvm.module.flags = !{!0, !1}
+!llvm.ident = !{!2}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 7, !"frame-pointer", i32 2}
+!2 = !{!"clang version 17.0.0 (https://github.com/llvm/llvm-project.git c102025a4299e74767cdb4dfba8abbf6cbad820b)"}
Index: llvm/test/CodeGen/BPF/movsx.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/movsx.ll
@@ -0,0 +1,86 @@
+; RUN: llc -march=bpfel -mcpu=v4 -verify-machineinstrs -show-mc-encoding < %s | FileCheck %s
+; Source:
+;   short f1(int a) {
+; return (char)a;
+;   }
+;   int f2(int a) {
+; return (short)a;
+;   }
+;   long f3(int a) {
+; return (char)a;
+;   }
+;   long f4(int a) {
+; return (short)a;
+;   }
+;   long f5(int a) {
+; return a;
+;   }
+;   long f6(long a) {
+; return (int)a;
+;   }
+; Compilation flags:
+;   clang -target bpf -O2 -mcpu=v4 -S -emit-llvm t.c
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
+define dso_local i16 @f1(i32 noundef %a) local_unnamed_addr #0 {
+entry:
+  %conv = trunc i32 %a to i8
+  %conv1 = sext i8 %conv to i16
+  ret i16 %conv1
+}
+; CHECK:  w0 = (s8)w1 # encoding: [0xbc,0x10,0x08,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
+define dso_local i32 @f2(i32 noundef %a) local_unnamed_addr #0 {
+entry:
+  %sext = shl i32 %a, 16
+  %conv1 = ashr exact i32 %sext, 16
+  ret i32 %conv1
+}
+; CHECK:  w0 = (s16)w1# encoding: [0xbc,0x10,0x10,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
+define dso_local i64 @f3(i32 noundef %a) local_unnamed_addr #0 {
+entry:
+  %conv = zext i32 %a to i64
+  %sext = shl i64 %conv, 56
+  

[PATCH] D144829: [BPF] Add a few new insns under cpu=v4

2023-07-24 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 543709.
yonghong-song added a comment.

Three major changes in this patch:

- for ldsx insns, remove 32bit ldsx insns (1-byte and 2-byte sign extension) 
since the ldsx insn expects to sign extension all the way up to 8-byte and 
normal 32bit insn (e.g. BPF_ALU) expects to zero out the top bits. Instead do a 
ldbsx/ldhsx and then take the lower 4 byte to extract 32bit value. This also 
resolved one disasm issue reported by Eduard.
- for movsx insn, for 32bit sign extenstion to 64bit. Match both "sext_inreg 
GPR:$src, i32" (left and right shifting) and "sext GPR32:$src".
- Add an internal flag to control when to generate gotol insns in 
BPFMIPeephole.cpp. This permits a simpler test for gotol insns.

With the above changes, the following change is needed:

  diff --git a/tools/testing/selftests/bpf/progs/verifier_movsx.c 
b/tools/testing/selftests/bpf/progs/verifier_movsx.c
  index 5ee7d004f8ba..e27bfa11c9b3 100644
  --- a/tools/testing/selftests/bpf/progs/verifier_movsx.c
  +++ b/tools/testing/selftests/bpf/progs/verifier_movsx.c
  @@ -59,7 +59,7 @@ __naked void mov64sx_s32(void)
   {
  asm volatile (" \
  r0 = 0xfffe;\
  -   r0 = (s32)w0;   \
  +   r0 = (s32)r0;   \
  r0 >>= 1;   \
  exit;   \
   "  ::: __clobber_all);
  @@ -181,7 +181,7 @@ __naked void mov64sx_s32_range(void)
   {
  asm volatile (" \
  call %[bpf_get_prandom_u32];\
  -   r1 = (s32)w0;   \
  +   r1 = (s32)r0;   \
  /* r1 with s32 range */ \
  if r1 s> 0x7fff goto l0_%=; \
  if r1 s< -0x8000 goto l0_%=;\

in order to compile kernel cpu v4 support (patch series v3)

  https://lore.kernel.org/bpf/2023072103.99949-1-...@fb.com/

I will update the kernel side once we resolved all llvm issues.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144829/new/

https://reviews.llvm.org/D144829

Files:
  clang/lib/Basic/Targets/BPF.cpp
  clang/lib/Basic/Targets/BPF.h
  clang/test/Misc/target-invalid-cpu-note.c
  llvm/lib/Target/BPF/AsmParser/BPFAsmParser.cpp
  llvm/lib/Target/BPF/BPF.td
  llvm/lib/Target/BPF/BPFISelDAGToDAG.cpp
  llvm/lib/Target/BPF/BPFISelLowering.cpp
  llvm/lib/Target/BPF/BPFISelLowering.h
  llvm/lib/Target/BPF/BPFInstrFormats.td
  llvm/lib/Target/BPF/BPFInstrInfo.td
  llvm/lib/Target/BPF/BPFMIPeephole.cpp
  llvm/lib/Target/BPF/BPFMISimplifyPatchable.cpp
  llvm/lib/Target/BPF/BPFSubtarget.cpp
  llvm/lib/Target/BPF/BPFSubtarget.h
  llvm/lib/Target/BPF/Disassembler/BPFDisassembler.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCFixups.h
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp
  llvm/test/CodeGen/BPF/assembler-disassembler-v4.s
  llvm/test/CodeGen/BPF/bswap.ll
  llvm/test/CodeGen/BPF/gotol.ll
  llvm/test/CodeGen/BPF/ldsx.ll
  llvm/test/CodeGen/BPF/movsx.ll
  llvm/test/CodeGen/BPF/sdiv_smod.ll

Index: llvm/test/CodeGen/BPF/sdiv_smod.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/sdiv_smod.ll
@@ -0,0 +1,44 @@
+; RUN: llc -march=bpfel -mcpu=v4 -verify-machineinstrs -show-mc-encoding < %s | FileCheck %s
+; Source:
+;  int foo(int a, int b, int c) {
+;return a/b + a%c;
+;  }
+;  long bar(long a, long b, long c) {
+;   return a/b + a%c;
+; }
+; Compilation flags:
+;   clang -target bpf -O2 -S -emit-llvm t.c
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
+define dso_local i32 @foo(i32 noundef %a, i32 noundef %b, i32 noundef %c) local_unnamed_addr #0 {
+entry:
+  %div = sdiv i32 %a, %b
+  %rem = srem i32 %a, %c
+  %add = add nsw i32 %rem, %div
+  ret i32 %add
+}
+
+; CHECK:   w0 = w1
+; CHECK-NEXT:  w1 s/= w2   # encoding: [0x3c,0x21,0x01,0x00,0x00,0x00,0x00,0x00]
+; CHECK-NEXT:  w0 s%= w3   # encoding: [0x9c,0x30,0x01,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
+define dso_local i64 @bar(i64 noundef %a, i64 noundef %b, i64 noundef %c) local_unnamed_addr #0 {
+entry:
+  %div = sdiv i64 %a, %b
+  %rem = srem i64 %a, %c
+  %add = add nsw i64 %rem, %div
+  ret i64 %add
+}
+; CHECK:   r0 = r1
+; CHECK-NEXT:  r1 s/= r2   # encoding: [0x3f,0x21,0x01,0x00,0x00,0x00,0x00,0x00]
+; CHECK-NEXT:  r0 s%= r3   # encoding: 

[PATCH] D144829: [BPF] Add a few new insns under cpu=v4

2023-07-24 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

> Could you please also add a few tests for `gotol`?

Will do!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144829/new/

https://reviews.llvm.org/D144829

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D144829: [BPF] Add a few new insns under cpu=v4

2023-07-24 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added inline comments.



Comment at: llvm/lib/Target/BPF/BPFInstrInfo.td:379
+  "$dst = (s8)$src",
+  [(set GPR:$dst, (sra (shl GPR:$src, (i64 56)), (i64 
56)))]>;
+  def MOVSX_rr_16 : ALU_RR I think it is possible to avoid matching expansion pattern `(sra (shl 
> GPR:$src, (i64 56))` here, and instead turn off the expansion when `movsx` is 
> available.
> 
> I tried the change below and all BPF codegen tests are passing. Do I miss 
> something?
> 
> ---
> 
> ```
> diff --git a/llvm/lib/Target/BPF/BPFISelLowering.cpp 
> b/llvm/lib/Target/BPF/BPFISelLowering.cpp
> index 9a7357d6ad04..5e84af009591 100644
> --- a/llvm/lib/Target/BPF/BPFISelLowering.cpp
> +++ b/llvm/lib/Target/BPF/BPFISelLowering.cpp
> @@ -132,9 +132,11 @@ BPFTargetLowering::BPFTargetLowering(const TargetMachine 
> ,
>setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
>  
>setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
> -  setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
> -  setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
> -  setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand);
> +  if (!STI.hasMovsx()) {
> +setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
> +setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
> +setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand);
> +  }
>  
>// Extended load operations for i1 types must be promoted
>for (MVT VT : MVT::integer_valuetypes()) {
> diff --git a/llvm/lib/Target/BPF/BPFInstrInfo.td 
> b/llvm/lib/Target/BPF/BPFInstrInfo.td
> index a1d532e60db2..29bec72aa92d 100644
> --- a/llvm/lib/Target/BPF/BPFInstrInfo.td
> +++ b/llvm/lib/Target/BPF/BPFInstrInfo.td
> @@ -376,11 +376,11 @@ let Predicates = [BPFHasMovsx] in {
>def MOVSX_rr_8 : ALU_RR(outs GPR:$dst), (ins GPR:$src),
>"$dst = (s8)$src",
> -  [(set GPR:$dst, (sra (shl GPR:$src, (i64 56)), (i64 
> 56)))]>;
> +  [(set GPR:$dst, (sext_inreg GPR:$src, i8))]>;
>def MOVSX_rr_16 : ALU_RR(outs GPR:$dst), (ins GPR:$src),
>"$dst = (s16)$src",
> -  [(set GPR:$dst, (sra (shl GPR:$src, (i64 48)), (i64 
> 48)))]>;
> +  [(set GPR:$dst, (sext_inreg GPR:$src, i16))]>;
>def MOVSX_rr_32 : ALU_RR(outs GPR:$dst), (ins GPR32:$src),
>"$dst = (s32)$src",
> @@ -388,11 +388,11 @@ let Predicates = [BPFHasMovsx] in {
>def MOVSX_rr_32_8 : ALU_RR(outs GPR32:$dst), (ins GPR32:$src),
>"$dst = (s8)$src",
> -  [(set GPR32:$dst, (sra (shl GPR32:$src, (i32 24)), 
> (i32 24)))]>;
> +  [(set GPR32:$dst, (sext_inreg GPR32:$src, i8))]>;
>def MOVSX_rr_32_16 : ALU_RR(outs GPR32:$dst), (ins GPR32:$src),
>"$dst = (s16)$src",
> -  [(set GPR32:$dst, (sra (shl GPR32:$src, (i32 16)), 
> (i32 16)))]>;
> +  [(set GPR32:$dst, (sext_inreg GPR32:$src, i16))]>;
>  }
>  }
>  ```
This indeed can simplify the code. I will incorporate your change into the 
patch. Thanks!



Comment at: llvm/lib/Target/BPF/BPFMIPeephole.cpp:321
+
+  std::map ReverseCondOpMap;
 

eddyz87 wrote:
> Is this map unused?
No. This is a leftover. Will remove.



Comment at: llvm/lib/Target/BPF/BPFMIPeephole.cpp:412
+  int CurrNumInsns = 0;
+  std::map SoFarNumInsns;
+  std::map FollowThroughBB;

eddyz87 wrote:
> Nitpick: Fangrui suggested in my llvm-objdump revisions to use `DenseMap` in 
> most cases (as `std::map` allocates for each pair).
Will try to use DenseMap.



Comment at: llvm/test/CodeGen/BPF/movsx.ll:30
+}
+; CHECK: w0 = w1 # encoding: 
[0xbc,0x10,0x00,0x00,0x00,0x00,0x00,0x00]
+

eddyz87 wrote:
> This does not seem right, as it does not sign extend 8-bit argument to 16-bit 
> value.
This is probably due to ABI. For example,
```
$ cat t1.c
__attribute__((noinline)) short f1(char a) {
  return a * a;
}

int f2(int a) {
  return f1(a);
}


$ clang --target=bpf -O2 -mcpu=v4 -S t1.c

f1: # @f1
# %bb.0:# %entry
w0 = w1
w0 *= w0
exit
.Lfunc_end0:
.size   f1, .Lfunc_end0-f1
# -- End function
.globl  f2  # -- Begin function f2
.p2align3
.type   f2,@function
f2: # @f2
# %bb.0:# %entry
w1 = (s8)w1
call f1
w0 = (s16)w0
exit
```
You can see in function f2(), the sign-extension has been done properly. and 
that is 

[PATCH] D144829: [BPF] Add a few new insns under cpu=v4

2023-07-24 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

In D144829#4519036 , @eddyz87 wrote:

> I tried adding a test similar to `assemble-disassemble.ll`:
>
>   // RUN: llvm-mc -triple bpfel --mcpu=v4 --assemble --filetype=obj %s \
>   // RUN:   | llvm-objdump -d --mattr=+alu32 - \
>   // RUN:   | FileCheck %s
>   
>   // CHECK: d7 01 00 00 10 00 00 00   r1 = bswap16 r1
>   // CHECK: d7 02 00 00 20 00 00 00   r2 = bswap32 r2
>   // CHECK: d7 03 00 00 40 00 00 00   r3 = bswap64 r3
>   r1 = bswap16 r1
>   r2 = bswap32 r2
>   r3 = bswap64 r3
>   
>   // CHECK: 91 41 00 00 00 00 00 00   r1 = *(s8 *)(r4 + 0x0)
>   // CHECK: 89 52 04 00 00 00 00 00   r2 = *(s16 *)(r5 + 0x4)
>   // CHECK: 81 63 08 00 00 00 00 00   r3 = *(s32 *)(r6 + 0x8)
>   r1 = *(s8 *)(r4 + 0)
>   r2 = *(s16 *)(r5 + 4)
>   r3 = *(s32 *)(r6 + 8)
>   
>   // CHECK: 91 41 00 00 00 00 00 00   w1 = *(s8 *)(r4 + 0x0)
>   // CHECK: 89 52 04 00 00 00 00 00   w2 = *(s16 *)(r5 + 0x4)
>   w1 = *(s8 *)(r4 + 0)
>   w2 = *(s16 *)(r5 + 4)
>   
>   // CHECK: bf 41 08 00 00 00 00 00   r1 = (s8)r4
>   // CHECK: bf 52 10 00 00 00 00 00   r2 = (s16)r5
>   // CHECK: bf 63 20 00 00 00 00 00   r3 = (s32)w6
>   r1 = (s8)r4
>   r2 = (s16)r5
>   r3 = (s32)w6
>   // Should this work as well: r3 = (s32)r6 ?
>   
>   // CHECK: bc 31 08 00 00 00 00 00   w1 = (s8)w3
>   // CHECK: bc 42 10 00 00 00 00 00   w2 = (s16)w4
>   w1 = (s8)w3
>   w2 = (s16)w4
>   
>   // CHECK: 3f 31 01 00 00 00 00 00   r1 s/= r3
>   // CHECK: 9f 42 01 00 00 00 00 00   r2 s%= r4
>   r1 s/= r3
>   r2 s%= r4
>   
>   // CHECK: 3c 31 01 00 00 00 00 00   w1 s/= w3
>   // CHECK: 9c 42 01 00 00 00 00 00   w2 s%= w4
>   w1 s/= w3
>   w2 s%= w4
>
> And it looks like some instructions are not printed correctly:
>
>   $ llvm-mc -triple bpfel --mcpu=v4 --assemble --filetype=obj 
> /home/eddy/work/llvm-project/llvm/test/CodeGen/BPF/assembler-disassembler-v4.s
>  | llvm-objdump -d --mattr=+alu32 -
>   
>   :file format elf64-bpf
>   
>   Disassembly of section .text:
>   
>    <.text>:
>  0:   d7 01 00 00 10 00 00 00 r1 = bswap16 r1
>  1:   d7 02 00 00 20 00 00 00 r2 = bswap32 r2
>  2:   d7 03 00 00 40 00 00 00 r3 = bswap64 r3
>  3:   91 41 00 00 00 00 00 00 w1 = *(s8 *)(r4 + 0x0)
>  4:   89 52 04 00 00 00 00 00 w2 = *(s16 *)(r5 + 0x4)
>  5:   81 63 08 00 00 00 00 00 
>  6:   91 41 00 00 00 00 00 00 w1 = *(s8 *)(r4 + 0x0)
>  7:   89 52 04 00 00 00 00 00 w2 = *(s16 *)(r5 + 0x4)
>  8:   bf 41 08 00 00 00 00 00 r1 = (s8)r4
>  9:   bf 52 10 00 00 00 00 00 r2 = (s16)r5
> 10:   bf 63 20 00 00 00 00 00 r3 = (s32)w6
> 11:   bc 31 08 00 00 00 00 00 w1 = (s8)w3
> 12:   bc 42 10 00 00 00 00 00 w2 = (s16)w4
> 13:   3f 31 01 00 00 00 00 00 r1 s/= r3
> 14:   9f 42 01 00 00 00 00 00 r2 s%= r4
> 15:   3c 31 01 00 00 00 00 00 w1 s/= w3
> 16:   9c 42 01 00 00 00 00 00 w2 s%= w4
>
> I'm not sure if this is an issue with disassembler or some additional 
> `--mattr` options are needed.

There is a problem in the td file for 32-bit signed load. Current definition is 
not quite right since it is supposed to sign-extension all the way to 64bit. I 
will fix it in the next revision.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144829/new/

https://reviews.llvm.org/D144829

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D144829: [WIP][BPF] Add a few new insns under cpu=v4

2023-07-19 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 542245.
yonghong-song added a comment.

- remove a copy-paste comment from s390 arch


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144829/new/

https://reviews.llvm.org/D144829

Files:
  clang/lib/Basic/Targets/BPF.cpp
  clang/lib/Basic/Targets/BPF.h
  clang/test/Misc/target-invalid-cpu-note.c
  llvm/lib/Target/BPF/AsmParser/BPFAsmParser.cpp
  llvm/lib/Target/BPF/BPF.td
  llvm/lib/Target/BPF/BPFISelDAGToDAG.cpp
  llvm/lib/Target/BPF/BPFISelLowering.cpp
  llvm/lib/Target/BPF/BPFISelLowering.h
  llvm/lib/Target/BPF/BPFInstrFormats.td
  llvm/lib/Target/BPF/BPFInstrInfo.td
  llvm/lib/Target/BPF/BPFMIPeephole.cpp
  llvm/lib/Target/BPF/BPFMISimplifyPatchable.cpp
  llvm/lib/Target/BPF/BPFSubtarget.cpp
  llvm/lib/Target/BPF/BPFSubtarget.h
  llvm/lib/Target/BPF/Disassembler/BPFDisassembler.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCFixups.h
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp
  llvm/test/CodeGen/BPF/bswap.ll
  llvm/test/CodeGen/BPF/ldsx.ll
  llvm/test/CodeGen/BPF/movsx.ll
  llvm/test/CodeGen/BPF/sdiv_smod.ll

Index: llvm/test/CodeGen/BPF/sdiv_smod.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/sdiv_smod.ll
@@ -0,0 +1,77 @@
+; RUN: llc -march=bpfel -mcpu=v4 -verify-machineinstrs -show-mc-encoding < %s | FileCheck %s
+; Source:
+;  int foo(int a, int b, int c) {
+;return a/b + a%c;
+;  }
+;  long bar(long a, long b, long c) {
+;   return a/b + a%c;
+; }
+; Compilation flags:
+;   clang -target bpf -O2 -S -emit-llvm -Xclang -disable-llvm-passes t.c
+
+; Function Attrs: nounwind
+define dso_local i32 @foo(i32 noundef %a, i32 noundef %b, i32 noundef %c) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  %b.addr = alloca i32, align 4
+  %c.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  store i32 %b, ptr %b.addr, align 4, !tbaa !3
+  store i32 %c, ptr %c.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %1 = load i32, ptr %b.addr, align 4, !tbaa !3
+  %div = sdiv i32 %0, %1
+  %2 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %3 = load i32, ptr %c.addr, align 4, !tbaa !3
+  %rem = srem i32 %2, %3
+  %add = add nsw i32 %div, %rem
+  ret i32 %add
+}
+
+; CHECK:   w0 = w1
+; CHECK-NEXT:  *(u32 *)(r10 - 8) = w2
+; CHECK-NEXT:  *(u32 *)(r10 - 4) = w0
+; CHECK-NEXT:  *(u32 *)(r10 - 12) = w3
+; CHECK-NEXT:  w1 s%= w3  # encoding: [0x9c,0x31,0x01,0x00,0x00,0x00,0x00,0x00]
+; CHECK-NEXT:  w0 s/= w2  # encoding: [0x3c,0x20,0x01,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i64 @bar(i64 noundef %a, i64 noundef %b, i64 noundef %c) #0 {
+entry:
+  %a.addr = alloca i64, align 8
+  %b.addr = alloca i64, align 8
+  %c.addr = alloca i64, align 8
+  store i64 %a, ptr %a.addr, align 8, !tbaa !7
+  store i64 %b, ptr %b.addr, align 8, !tbaa !7
+  store i64 %c, ptr %c.addr, align 8, !tbaa !7
+  %0 = load i64, ptr %a.addr, align 8, !tbaa !7
+  %1 = load i64, ptr %b.addr, align 8, !tbaa !7
+  %div = sdiv i64 %0, %1
+  %2 = load i64, ptr %a.addr, align 8, !tbaa !7
+  %3 = load i64, ptr %c.addr, align 8, !tbaa !7
+  %rem = srem i64 %2, %3
+  %add = add nsw i64 %div, %rem
+  ret i64 %add
+}
+
+; CHECK:   r0 = r1
+; CHECK-NEXT:  *(u64 *)(r10 - 16) = r2
+; CHECK-NEXT:  *(u64 *)(r10 - 8) = r0
+; CHECK-NEXT:  *(u64 *)(r10 - 24) = r3
+; CHECK-NEXT:  r1 s%= r3  # encoding: [0x9f,0x31,0x01,0x00,0x00,0x00,0x00,0x00]
+; CHECK-NEXT:  r0 s/= r2  # encoding: [0x3f,0x20,0x01,0x00,0x00,0x00,0x00,0x00]
+
+attributes #0 = { nounwind "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+
+!llvm.module.flags = !{!0, !1}
+!llvm.ident = !{!2}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 7, !"frame-pointer", i32 2}
+!2 = !{!"clang version 17.0.0 (https://github.com/llvm/llvm-project.git 569bd3b841e3167ddd7c6ceeddb282d3c280e761)"}
+!3 = !{!4, !4, i64 0}
+!4 = !{!"int", !5, i64 0}
+!5 = !{!"omnipotent char", !6, i64 0}
+!6 = !{!"Simple C/C++ TBAA"}
+!7 = !{!8, !8, i64 0}
+!8 = !{!"long", !5, i64 0}
Index: llvm/test/CodeGen/BPF/movsx.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/movsx.ll
@@ -0,0 +1,79 @@
+; RUN: llc -march=bpfel -mcpu=v4 -verify-machineinstrs -show-mc-encoding < %s | FileCheck %s
+; Source:
+;   short f1(char a) {
+; return a;
+;   }
+;   int f2(char a) {
+; return a;
+;   }
+;   long f3(char a) {
+; return a;
+;   }
+;   int f4(short a) {
+; return a;
+;   }
+;   long f5(short a) {
+; return a;
+;   }
+;   long f6(int a) {
+; return a;
+;   }
+; Compilation flags:
+;   clang -target bpf -O2 -S -emit-llvm t.c
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)

[PATCH] D144829: [WIP][BPF] Add a few new insns under cpu=v4

2023-07-17 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 541265.
yonghong-song edited the summary of this revision.
yonghong-song added a comment.

- Dropping 'CPUv4' in some variable/function names and also in debug flags.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144829/new/

https://reviews.llvm.org/D144829

Files:
  clang/lib/Basic/Targets/BPF.cpp
  clang/lib/Basic/Targets/BPF.h
  clang/test/Misc/target-invalid-cpu-note.c
  llvm/lib/Target/BPF/AsmParser/BPFAsmParser.cpp
  llvm/lib/Target/BPF/BPF.td
  llvm/lib/Target/BPF/BPFISelDAGToDAG.cpp
  llvm/lib/Target/BPF/BPFISelLowering.cpp
  llvm/lib/Target/BPF/BPFISelLowering.h
  llvm/lib/Target/BPF/BPFInstrFormats.td
  llvm/lib/Target/BPF/BPFInstrInfo.td
  llvm/lib/Target/BPF/BPFMIPeephole.cpp
  llvm/lib/Target/BPF/BPFMISimplifyPatchable.cpp
  llvm/lib/Target/BPF/BPFSubtarget.cpp
  llvm/lib/Target/BPF/BPFSubtarget.h
  llvm/lib/Target/BPF/Disassembler/BPFDisassembler.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCFixups.h
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp
  llvm/test/CodeGen/BPF/bswap.ll
  llvm/test/CodeGen/BPF/ldsx.ll
  llvm/test/CodeGen/BPF/movsx.ll
  llvm/test/CodeGen/BPF/sdiv_smod.ll

Index: llvm/test/CodeGen/BPF/sdiv_smod.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/sdiv_smod.ll
@@ -0,0 +1,77 @@
+; RUN: llc -march=bpfel -mcpu=v4 -verify-machineinstrs -show-mc-encoding < %s | FileCheck %s
+; Source:
+;  int foo(int a, int b, int c) {
+;return a/b + a%c;
+;  }
+;  long bar(long a, long b, long c) {
+;   return a/b + a%c;
+; }
+; Compilation flags:
+;   clang -target bpf -O2 -S -emit-llvm -Xclang -disable-llvm-passes t.c
+
+; Function Attrs: nounwind
+define dso_local i32 @foo(i32 noundef %a, i32 noundef %b, i32 noundef %c) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  %b.addr = alloca i32, align 4
+  %c.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  store i32 %b, ptr %b.addr, align 4, !tbaa !3
+  store i32 %c, ptr %c.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %1 = load i32, ptr %b.addr, align 4, !tbaa !3
+  %div = sdiv i32 %0, %1
+  %2 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %3 = load i32, ptr %c.addr, align 4, !tbaa !3
+  %rem = srem i32 %2, %3
+  %add = add nsw i32 %div, %rem
+  ret i32 %add
+}
+
+; CHECK:   w0 = w1
+; CHECK-NEXT:  *(u32 *)(r10 - 8) = w2
+; CHECK-NEXT:  *(u32 *)(r10 - 4) = w0
+; CHECK-NEXT:  *(u32 *)(r10 - 12) = w3
+; CHECK-NEXT:  w1 s%= w3  # encoding: [0x9c,0x31,0x01,0x00,0x00,0x00,0x00,0x00]
+; CHECK-NEXT:  w0 s/= w2  # encoding: [0x3c,0x20,0x01,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i64 @bar(i64 noundef %a, i64 noundef %b, i64 noundef %c) #0 {
+entry:
+  %a.addr = alloca i64, align 8
+  %b.addr = alloca i64, align 8
+  %c.addr = alloca i64, align 8
+  store i64 %a, ptr %a.addr, align 8, !tbaa !7
+  store i64 %b, ptr %b.addr, align 8, !tbaa !7
+  store i64 %c, ptr %c.addr, align 8, !tbaa !7
+  %0 = load i64, ptr %a.addr, align 8, !tbaa !7
+  %1 = load i64, ptr %b.addr, align 8, !tbaa !7
+  %div = sdiv i64 %0, %1
+  %2 = load i64, ptr %a.addr, align 8, !tbaa !7
+  %3 = load i64, ptr %c.addr, align 8, !tbaa !7
+  %rem = srem i64 %2, %3
+  %add = add nsw i64 %div, %rem
+  ret i64 %add
+}
+
+; CHECK:   r0 = r1
+; CHECK-NEXT:  *(u64 *)(r10 - 16) = r2
+; CHECK-NEXT:  *(u64 *)(r10 - 8) = r0
+; CHECK-NEXT:  *(u64 *)(r10 - 24) = r3
+; CHECK-NEXT:  r1 s%= r3  # encoding: [0x9f,0x31,0x01,0x00,0x00,0x00,0x00,0x00]
+; CHECK-NEXT:  r0 s/= r2  # encoding: [0x3f,0x20,0x01,0x00,0x00,0x00,0x00,0x00]
+
+attributes #0 = { nounwind "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+
+!llvm.module.flags = !{!0, !1}
+!llvm.ident = !{!2}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 7, !"frame-pointer", i32 2}
+!2 = !{!"clang version 17.0.0 (https://github.com/llvm/llvm-project.git 569bd3b841e3167ddd7c6ceeddb282d3c280e761)"}
+!3 = !{!4, !4, i64 0}
+!4 = !{!"int", !5, i64 0}
+!5 = !{!"omnipotent char", !6, i64 0}
+!6 = !{!"Simple C/C++ TBAA"}
+!7 = !{!8, !8, i64 0}
+!8 = !{!"long", !5, i64 0}
Index: llvm/test/CodeGen/BPF/movsx.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/movsx.ll
@@ -0,0 +1,79 @@
+; RUN: llc -march=bpfel -mcpu=v4 -verify-machineinstrs -show-mc-encoding < %s | FileCheck %s
+; Source:
+;   short f1(char a) {
+; return a;
+;   }
+;   int f2(char a) {
+; return a;
+;   }
+;   long f3(char a) {
+; return a;
+;   }
+;   int f4(short a) {
+; return a;
+;   }
+;   long f5(short a) {
+; return a;
+;   }
+;   long f6(int a) {
+; return a;
+;   }
+; Compilation flags:
+;   clang -target bpf -O2 -S -emit-llvm t.c
+
+; Function 

[PATCH] D144829: [WIP][BPF] Add a few new insns under cpu=v4

2023-07-14 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added inline comments.



Comment at: llvm/lib/Target/BPF/BPFInstrInfo.td:56
 def BPFNoALU32 : Predicate<"!Subtarget->getHasAlu32()">;
+def BPFHasCPUv4_ldsx : Predicate<"Subtarget->getCPUv4_ldsx()">;
+def BPFHasCPUv4_movsx : Predicate<"Subtarget->getCPUv4_movsx()">;

ast wrote:
> Here and elsewhere... let's drop CPUv4 mid prefix. imo the extra verbosity 
> doesn't improve readability.
> Same with the flag: disable-cpuv4-movsx. I can be disable-movsx.
> 
> s/BPFHasCPUv4_ldsx/BPFHasLdsx/
> s/getCPUv4_bswap/getHasBswap/ or even shorter hasBswap ?
Make sense. Will do. Ya, hasBswap is good enough to capture what it means.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144829/new/

https://reviews.llvm.org/D144829

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D144829: [WIP][BPF] Add a few new insns under cpu=v4

2023-07-12 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 539842.
yonghong-song edited the summary of this revision.
yonghong-song added a comment.

- rename some insn names or mode names (movs -> movsx, lds -> ldsx, MEMS -> 
MEMSX) etc to be consistent with kernel.
- add 5 llc flags to control on/off for each kind of insn (sdiv/smod, ldsx, 
movsx, bswap, gotol) to debugging purpose.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144829/new/

https://reviews.llvm.org/D144829

Files:
  clang/lib/Basic/Targets/BPF.cpp
  clang/lib/Basic/Targets/BPF.h
  clang/test/Misc/target-invalid-cpu-note.c
  llvm/lib/Target/BPF/AsmParser/BPFAsmParser.cpp
  llvm/lib/Target/BPF/BPF.td
  llvm/lib/Target/BPF/BPFISelDAGToDAG.cpp
  llvm/lib/Target/BPF/BPFISelLowering.cpp
  llvm/lib/Target/BPF/BPFISelLowering.h
  llvm/lib/Target/BPF/BPFInstrFormats.td
  llvm/lib/Target/BPF/BPFInstrInfo.td
  llvm/lib/Target/BPF/BPFMIPeephole.cpp
  llvm/lib/Target/BPF/BPFMISimplifyPatchable.cpp
  llvm/lib/Target/BPF/BPFSubtarget.cpp
  llvm/lib/Target/BPF/BPFSubtarget.h
  llvm/lib/Target/BPF/Disassembler/BPFDisassembler.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCFixups.h
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp
  llvm/test/CodeGen/BPF/bswap.ll
  llvm/test/CodeGen/BPF/ldsx.ll
  llvm/test/CodeGen/BPF/movsx.ll
  llvm/test/CodeGen/BPF/sdiv_smod.ll

Index: llvm/test/CodeGen/BPF/sdiv_smod.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/sdiv_smod.ll
@@ -0,0 +1,77 @@
+; RUN: llc -march=bpfel -mcpu=v4 -verify-machineinstrs -show-mc-encoding < %s | FileCheck %s
+; Source:
+;  int foo(int a, int b, int c) {
+;return a/b + a%c;
+;  }
+;  long bar(long a, long b, long c) {
+;   return a/b + a%c;
+; }
+; Compilation flags:
+;   clang -target bpf -O2 -S -emit-llvm -Xclang -disable-llvm-passes t.c
+
+; Function Attrs: nounwind
+define dso_local i32 @foo(i32 noundef %a, i32 noundef %b, i32 noundef %c) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  %b.addr = alloca i32, align 4
+  %c.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  store i32 %b, ptr %b.addr, align 4, !tbaa !3
+  store i32 %c, ptr %c.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %1 = load i32, ptr %b.addr, align 4, !tbaa !3
+  %div = sdiv i32 %0, %1
+  %2 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %3 = load i32, ptr %c.addr, align 4, !tbaa !3
+  %rem = srem i32 %2, %3
+  %add = add nsw i32 %div, %rem
+  ret i32 %add
+}
+
+; CHECK:   w0 = w1
+; CHECK-NEXT:  *(u32 *)(r10 - 8) = w2
+; CHECK-NEXT:  *(u32 *)(r10 - 4) = w0
+; CHECK-NEXT:  *(u32 *)(r10 - 12) = w3
+; CHECK-NEXT:  w1 s%= w3  # encoding: [0x9c,0x31,0x01,0x00,0x00,0x00,0x00,0x00]
+; CHECK-NEXT:  w0 s/= w2  # encoding: [0x3c,0x20,0x01,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i64 @bar(i64 noundef %a, i64 noundef %b, i64 noundef %c) #0 {
+entry:
+  %a.addr = alloca i64, align 8
+  %b.addr = alloca i64, align 8
+  %c.addr = alloca i64, align 8
+  store i64 %a, ptr %a.addr, align 8, !tbaa !7
+  store i64 %b, ptr %b.addr, align 8, !tbaa !7
+  store i64 %c, ptr %c.addr, align 8, !tbaa !7
+  %0 = load i64, ptr %a.addr, align 8, !tbaa !7
+  %1 = load i64, ptr %b.addr, align 8, !tbaa !7
+  %div = sdiv i64 %0, %1
+  %2 = load i64, ptr %a.addr, align 8, !tbaa !7
+  %3 = load i64, ptr %c.addr, align 8, !tbaa !7
+  %rem = srem i64 %2, %3
+  %add = add nsw i64 %div, %rem
+  ret i64 %add
+}
+
+; CHECK:   r0 = r1
+; CHECK-NEXT:  *(u64 *)(r10 - 16) = r2
+; CHECK-NEXT:  *(u64 *)(r10 - 8) = r0
+; CHECK-NEXT:  *(u64 *)(r10 - 24) = r3
+; CHECK-NEXT:  r1 s%= r3  # encoding: [0x9f,0x31,0x01,0x00,0x00,0x00,0x00,0x00]
+; CHECK-NEXT:  r0 s/= r2  # encoding: [0x3f,0x20,0x01,0x00,0x00,0x00,0x00,0x00]
+
+attributes #0 = { nounwind "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+
+!llvm.module.flags = !{!0, !1}
+!llvm.ident = !{!2}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 7, !"frame-pointer", i32 2}
+!2 = !{!"clang version 17.0.0 (https://github.com/llvm/llvm-project.git 569bd3b841e3167ddd7c6ceeddb282d3c280e761)"}
+!3 = !{!4, !4, i64 0}
+!4 = !{!"int", !5, i64 0}
+!5 = !{!"omnipotent char", !6, i64 0}
+!6 = !{!"Simple C/C++ TBAA"}
+!7 = !{!8, !8, i64 0}
+!8 = !{!"long", !5, i64 0}
Index: llvm/test/CodeGen/BPF/movsx.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/movsx.ll
@@ -0,0 +1,79 @@
+; RUN: llc -march=bpfel -mcpu=v4 -verify-machineinstrs -show-mc-encoding < %s | FileCheck %s
+; Source:
+;   short f1(char a) {
+; return a;
+;   }
+;   int f2(char a) {
+; return a;
+;   }
+;   long f3(char a) {
+; return a;
+;   }
+;   int f4(short a) {
+; return a;
+;   }
+;   long 

[PATCH] D144829: [WIP][BPF] Add a few new insns under cpu=v4

2023-07-02 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

In D144829#4465193 , @eddyz87 wrote:

> Hi Yonghong,
>
> What is the current plan for these changes?
> I'd like to refresh D140804  to make BPF_ST 
> instruction available for cpu v4.
> I see that the latest CI run failed because of libcxx issue, I think it is 
> completely unrelated to this revision, see here 
> .

Eduard, please go ahead to refresh BPF_ST patch (D140804 
). For this patch, we will land it until the 
kernel patch addressed all major comments. When it is ready to merge, you will 
comment and let you know.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144829/new/

https://reviews.llvm.org/D144829

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153898: [DebugInfo] Enable debug info emission for extern variables in C++

2023-06-29 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song accepted this revision.
yonghong-song added a comment.

I am okay with this change. Once you used this patch and implemented the 
mechanism inside Google. Could you send a follow-up summary on what the 
implementation looks like in Google? This will give people a sense why this is 
useful/better than other alternatives (like macro based approach).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153898/new/

https://reviews.llvm.org/D153898

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D144829: [WIP][BPF] Add a few new insns under cpu=v4

2023-06-26 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 534801.
yonghong-song added a comment.

- avoid changing conditions during JMP -> JMPL conversion. Otherwise, 
verification may fail in some cases.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144829/new/

https://reviews.llvm.org/D144829

Files:
  clang/lib/Basic/Targets/BPF.cpp
  clang/lib/Basic/Targets/BPF.h
  clang/test/Misc/target-invalid-cpu-note.c
  llvm/lib/Target/BPF/AsmParser/BPFAsmParser.cpp
  llvm/lib/Target/BPF/BPF.td
  llvm/lib/Target/BPF/BPFISelDAGToDAG.cpp
  llvm/lib/Target/BPF/BPFISelLowering.cpp
  llvm/lib/Target/BPF/BPFInstrFormats.td
  llvm/lib/Target/BPF/BPFInstrInfo.td
  llvm/lib/Target/BPF/BPFMIPeephole.cpp
  llvm/lib/Target/BPF/BPFMISimplifyPatchable.cpp
  llvm/lib/Target/BPF/BPFSubtarget.cpp
  llvm/lib/Target/BPF/BPFSubtarget.h
  llvm/lib/Target/BPF/Disassembler/BPFDisassembler.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCFixups.h
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp
  llvm/test/CodeGen/BPF/bswap.ll
  llvm/test/CodeGen/BPF/sdiv_smod.ll
  llvm/test/CodeGen/BPF/sext_ld.ll
  llvm/test/CodeGen/BPF/sext_mov.ll

Index: llvm/test/CodeGen/BPF/sext_mov.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/sext_mov.ll
@@ -0,0 +1,109 @@
+; RUN: llc -march=bpfel -mcpu=v4 -verify-machineinstrs -show-mc-encoding < %s | FileCheck %s
+; Source:
+;  short f1(int a) {
+;return (char)a;
+;  }
+;  int f2(int a) {
+;return (char)a;
+;  }
+;  long f3(int a) {
+;return (char)a;
+;  }
+;  int f4(int a) {
+;return (short)a;
+;  }
+;  long f5(int a) {
+;return (short)a;
+;  }
+;  long f6(long a) {
+;return (int)a;
+;  }
+; Compilation flags:
+;   clang -target bpf -O2 -S -emit-llvm -Xclang -disable-llvm-passes t.c
+
+; Function Attrs: nounwind
+define dso_local i16 @f1(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i8
+  %conv1 = sext i8 %conv to i16
+  ret i16 %conv1
+}
+; CHECK: w0 = (s8)w1  # encoding: [0xbc,0x10,0x08,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i32 @f2(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i8
+  %conv1 = sext i8 %conv to i32
+  ret i32 %conv1
+}
+; CHECK: w0 = (s8)w1  # encoding: [0xbc,0x10,0x08,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i64 @f3(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i8
+  %conv1 = sext i8 %conv to i64
+  ret i64 %conv1
+}
+; CHECK: r0 = (s8)r1  # encoding: [0xbf,0x10,0x08,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i32 @f4(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i16
+  %conv1 = sext i16 %conv to i32
+  ret i32 %conv1
+}
+; CHECK: w0 = (s16)w1  # encoding: [0xbc,0x10,0x10,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i64 @f5(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i16
+  %conv1 = sext i16 %conv to i64
+  ret i64 %conv1
+}
+; CHECK: r0 = (s16)r1  # encoding: [0xbf,0x10,0x10,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i64 @f6(i64 noundef %a) #0 {
+entry:
+  %a.addr = alloca i64, align 8
+  store i64 %a, ptr %a.addr, align 8, !tbaa !7
+  %0 = load i64, ptr %a.addr, align 8, !tbaa !7
+  %conv = trunc i64 %0 to i32
+  %conv1 = sext i32 %conv to i64
+  ret i64 %conv1
+}
+; CHECK: r0 = (s32)r1  # encoding: [0xbf,0x10,0x20,0x00,0x00,0x00,0x00,0x00]
+
+attributes #0 = { nounwind "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+
+!llvm.module.flags = !{!0, !1}
+!llvm.ident = !{!2}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 7, !"frame-pointer", i32 2}
+!2 = !{!"clang version 17.0.0 (https://github.com/llvm/llvm-project.git 569bd3b841e3167ddd7c6ceeddb282d3c280e761)"}
+!3 = !{!4, !4, i64 0}
+!4 = !{!"int", !5, i64 0}
+!5 = !{!"omnipotent char", !6, i64 0}
+!6 = !{!"Simple C/C++ TBAA"}
+!7 = !{!8, !8, i64 0}
+!8 = !{!"long", !5, i64 0}
Index: llvm/test/CodeGen/BPF/sext_ld.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/sext_ld.ll
@@ -0,0 +1,104 @@
+; RUN: llc -march=bpfel 

[PATCH] D144829: [WIP][BPF] Add a few new insns under cpu=v4

2023-06-25 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 534361.
yonghong-song added a comment.

- added support of new instructions in inline assembly.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144829/new/

https://reviews.llvm.org/D144829

Files:
  clang/lib/Basic/Targets/BPF.cpp
  clang/lib/Basic/Targets/BPF.h
  clang/test/Misc/target-invalid-cpu-note.c
  llvm/lib/Target/BPF/AsmParser/BPFAsmParser.cpp
  llvm/lib/Target/BPF/BPF.td
  llvm/lib/Target/BPF/BPFISelDAGToDAG.cpp
  llvm/lib/Target/BPF/BPFISelLowering.cpp
  llvm/lib/Target/BPF/BPFInstrFormats.td
  llvm/lib/Target/BPF/BPFInstrInfo.td
  llvm/lib/Target/BPF/BPFMIPeephole.cpp
  llvm/lib/Target/BPF/BPFMISimplifyPatchable.cpp
  llvm/lib/Target/BPF/BPFSubtarget.cpp
  llvm/lib/Target/BPF/BPFSubtarget.h
  llvm/lib/Target/BPF/Disassembler/BPFDisassembler.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCFixups.h
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp
  llvm/test/CodeGen/BPF/bswap.ll
  llvm/test/CodeGen/BPF/sdiv_smod.ll
  llvm/test/CodeGen/BPF/sext_ld.ll
  llvm/test/CodeGen/BPF/sext_mov.ll

Index: llvm/test/CodeGen/BPF/sext_mov.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/sext_mov.ll
@@ -0,0 +1,109 @@
+; RUN: llc -march=bpfel -mcpu=v4 -verify-machineinstrs -show-mc-encoding < %s | FileCheck %s
+; Source:
+;  short f1(int a) {
+;return (char)a;
+;  }
+;  int f2(int a) {
+;return (char)a;
+;  }
+;  long f3(int a) {
+;return (char)a;
+;  }
+;  int f4(int a) {
+;return (short)a;
+;  }
+;  long f5(int a) {
+;return (short)a;
+;  }
+;  long f6(long a) {
+;return (int)a;
+;  }
+; Compilation flags:
+;   clang -target bpf -O2 -S -emit-llvm -Xclang -disable-llvm-passes t.c
+
+; Function Attrs: nounwind
+define dso_local i16 @f1(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i8
+  %conv1 = sext i8 %conv to i16
+  ret i16 %conv1
+}
+; CHECK: w0 = (s8)w1  # encoding: [0xbc,0x10,0x08,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i32 @f2(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i8
+  %conv1 = sext i8 %conv to i32
+  ret i32 %conv1
+}
+; CHECK: w0 = (s8)w1  # encoding: [0xbc,0x10,0x08,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i64 @f3(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i8
+  %conv1 = sext i8 %conv to i64
+  ret i64 %conv1
+}
+; CHECK: r0 = (s8)r1  # encoding: [0xbf,0x10,0x08,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i32 @f4(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i16
+  %conv1 = sext i16 %conv to i32
+  ret i32 %conv1
+}
+; CHECK: w0 = (s16)w1  # encoding: [0xbc,0x10,0x10,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i64 @f5(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i16
+  %conv1 = sext i16 %conv to i64
+  ret i64 %conv1
+}
+; CHECK: r0 = (s16)r1  # encoding: [0xbf,0x10,0x10,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i64 @f6(i64 noundef %a) #0 {
+entry:
+  %a.addr = alloca i64, align 8
+  store i64 %a, ptr %a.addr, align 8, !tbaa !7
+  %0 = load i64, ptr %a.addr, align 8, !tbaa !7
+  %conv = trunc i64 %0 to i32
+  %conv1 = sext i32 %conv to i64
+  ret i64 %conv1
+}
+; CHECK: r0 = (s32)r1  # encoding: [0xbf,0x10,0x20,0x00,0x00,0x00,0x00,0x00]
+
+attributes #0 = { nounwind "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+
+!llvm.module.flags = !{!0, !1}
+!llvm.ident = !{!2}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 7, !"frame-pointer", i32 2}
+!2 = !{!"clang version 17.0.0 (https://github.com/llvm/llvm-project.git 569bd3b841e3167ddd7c6ceeddb282d3c280e761)"}
+!3 = !{!4, !4, i64 0}
+!4 = !{!"int", !5, i64 0}
+!5 = !{!"omnipotent char", !6, i64 0}
+!6 = !{!"Simple C/C++ TBAA"}
+!7 = !{!8, !8, i64 0}
+!8 = !{!"long", !5, i64 0}
Index: llvm/test/CodeGen/BPF/sext_ld.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/sext_ld.ll
@@ -0,0 +1,104 @@
+; RUN: llc -march=bpfel -mcpu=v4 -verify-machineinstrs -show-mc-encoding < 

[PATCH] D144829: [WIP][BPF] Add a few new insns under cpu=v4

2023-05-18 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 523569.
yonghong-song edited the summary of this revision.
yonghong-song added a comment.

- Fixed previous llvm-objdump issue for '> 16bit' 'gotol' insns.
- Now basic functionality for cpu=v4 should be complete for llvm, further work 
will focus on kernel.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144829/new/

https://reviews.llvm.org/D144829

Files:
  clang/lib/Basic/Targets/BPF.cpp
  clang/lib/Basic/Targets/BPF.h
  clang/test/Misc/target-invalid-cpu-note.c
  llvm/lib/Target/BPF/BPF.td
  llvm/lib/Target/BPF/BPFISelDAGToDAG.cpp
  llvm/lib/Target/BPF/BPFISelLowering.cpp
  llvm/lib/Target/BPF/BPFInstrFormats.td
  llvm/lib/Target/BPF/BPFInstrInfo.td
  llvm/lib/Target/BPF/BPFMIPeephole.cpp
  llvm/lib/Target/BPF/BPFSubtarget.cpp
  llvm/lib/Target/BPF/BPFSubtarget.h
  llvm/lib/Target/BPF/Disassembler/BPFDisassembler.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCFixups.h
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp
  llvm/test/CodeGen/BPF/bswap.ll
  llvm/test/CodeGen/BPF/sdiv_smod.ll
  llvm/test/CodeGen/BPF/sext_ld.ll
  llvm/test/CodeGen/BPF/sext_mov.ll

Index: llvm/test/CodeGen/BPF/sext_mov.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/sext_mov.ll
@@ -0,0 +1,109 @@
+; RUN: llc -march=bpfel -mcpu=v4 -verify-machineinstrs -show-mc-encoding < %s | FileCheck %s
+; Source:
+;  short f1(int a) {
+;return (char)a;
+;  }
+;  int f2(int a) {
+;return (char)a;
+;  }
+;  long f3(int a) {
+;return (char)a;
+;  }
+;  int f4(int a) {
+;return (short)a;
+;  }
+;  long f5(int a) {
+;return (short)a;
+;  }
+;  long f6(long a) {
+;return (int)a;
+;  }
+; Compilation flags:
+;   clang -target bpf -O2 -S -emit-llvm -Xclang -disable-llvm-passes t.c
+
+; Function Attrs: nounwind
+define dso_local i16 @f1(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i8
+  %conv1 = sext i8 %conv to i16
+  ret i16 %conv1
+}
+; CHECK: w0 = (s8)w1  # encoding: [0xbc,0x10,0x08,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i32 @f2(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i8
+  %conv1 = sext i8 %conv to i32
+  ret i32 %conv1
+}
+; CHECK: w0 = (s8)w1  # encoding: [0xbc,0x10,0x08,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i64 @f3(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i8
+  %conv1 = sext i8 %conv to i64
+  ret i64 %conv1
+}
+; CHECK: r0 = (s8)r1  # encoding: [0xbf,0x10,0x08,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i32 @f4(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i16
+  %conv1 = sext i16 %conv to i32
+  ret i32 %conv1
+}
+; CHECK: w0 = (s16)w1  # encoding: [0xbc,0x10,0x10,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i64 @f5(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i16
+  %conv1 = sext i16 %conv to i64
+  ret i64 %conv1
+}
+; CHECK: r0 = (s16)r1  # encoding: [0xbf,0x10,0x10,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i64 @f6(i64 noundef %a) #0 {
+entry:
+  %a.addr = alloca i64, align 8
+  store i64 %a, ptr %a.addr, align 8, !tbaa !7
+  %0 = load i64, ptr %a.addr, align 8, !tbaa !7
+  %conv = trunc i64 %0 to i32
+  %conv1 = sext i32 %conv to i64
+  ret i64 %conv1
+}
+; CHECK: r0 = (s32)r1  # encoding: [0xbf,0x10,0x20,0x00,0x00,0x00,0x00,0x00]
+
+attributes #0 = { nounwind "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+
+!llvm.module.flags = !{!0, !1}
+!llvm.ident = !{!2}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 7, !"frame-pointer", i32 2}
+!2 = !{!"clang version 17.0.0 (https://github.com/llvm/llvm-project.git 569bd3b841e3167ddd7c6ceeddb282d3c280e761)"}
+!3 = !{!4, !4, i64 0}
+!4 = !{!"int", !5, i64 0}
+!5 = !{!"omnipotent char", !6, i64 0}
+!6 = !{!"Simple C/C++ TBAA"}
+!7 = !{!8, !8, i64 0}
+!8 = !{!"long", !5, i64 0}
Index: llvm/test/CodeGen/BPF/sext_ld.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/sext_ld.ll
@@ -0,0 +1,104 @@
+; RUN: llc 

[PATCH] D144829: [WIP][BPF] Add a few new insns under cpu=v4

2023-05-17 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 523253.
yonghong-song added a comment.

- Fixed issues reported by Eduard
- llvm-objdump issue (as stated in 'Summary') is not resolved yet.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144829/new/

https://reviews.llvm.org/D144829

Files:
  clang/lib/Basic/Targets/BPF.cpp
  clang/lib/Basic/Targets/BPF.h
  clang/test/Misc/target-invalid-cpu-note.c
  llvm/lib/Target/BPF/BPF.td
  llvm/lib/Target/BPF/BPFISelDAGToDAG.cpp
  llvm/lib/Target/BPF/BPFISelLowering.cpp
  llvm/lib/Target/BPF/BPFInstrFormats.td
  llvm/lib/Target/BPF/BPFInstrInfo.cpp
  llvm/lib/Target/BPF/BPFInstrInfo.td
  llvm/lib/Target/BPF/BPFMIPeephole.cpp
  llvm/lib/Target/BPF/BPFSubtarget.cpp
  llvm/lib/Target/BPF/BPFSubtarget.h
  llvm/lib/Target/BPF/Disassembler/BPFDisassembler.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCFixups.h
  llvm/test/CodeGen/BPF/bswap.ll
  llvm/test/CodeGen/BPF/sdiv_smod.ll
  llvm/test/CodeGen/BPF/sext_ld.ll
  llvm/test/CodeGen/BPF/sext_mov.ll

Index: llvm/test/CodeGen/BPF/sext_mov.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/sext_mov.ll
@@ -0,0 +1,109 @@
+; RUN: llc -march=bpfel -mcpu=v4 -verify-machineinstrs -show-mc-encoding < %s | FileCheck %s
+; Source:
+;  short f1(int a) {
+;return (char)a;
+;  }
+;  int f2(int a) {
+;return (char)a;
+;  }
+;  long f3(int a) {
+;return (char)a;
+;  }
+;  int f4(int a) {
+;return (short)a;
+;  }
+;  long f5(int a) {
+;return (short)a;
+;  }
+;  long f6(long a) {
+;return (int)a;
+;  }
+; Compilation flags:
+;   clang -target bpf -O2 -S -emit-llvm -Xclang -disable-llvm-passes t.c
+
+; Function Attrs: nounwind
+define dso_local i16 @f1(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i8
+  %conv1 = sext i8 %conv to i16
+  ret i16 %conv1
+}
+; CHECK: w0 = (s8)w1  # encoding: [0xbc,0x10,0x08,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i32 @f2(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i8
+  %conv1 = sext i8 %conv to i32
+  ret i32 %conv1
+}
+; CHECK: w0 = (s8)w1  # encoding: [0xbc,0x10,0x08,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i64 @f3(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i8
+  %conv1 = sext i8 %conv to i64
+  ret i64 %conv1
+}
+; CHECK: r0 = (s8)r1  # encoding: [0xbf,0x10,0x08,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i32 @f4(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i16
+  %conv1 = sext i16 %conv to i32
+  ret i32 %conv1
+}
+; CHECK: w0 = (s16)w1  # encoding: [0xbc,0x10,0x10,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i64 @f5(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i16
+  %conv1 = sext i16 %conv to i64
+  ret i64 %conv1
+}
+; CHECK: r0 = (s16)r1  # encoding: [0xbf,0x10,0x10,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i64 @f6(i64 noundef %a) #0 {
+entry:
+  %a.addr = alloca i64, align 8
+  store i64 %a, ptr %a.addr, align 8, !tbaa !7
+  %0 = load i64, ptr %a.addr, align 8, !tbaa !7
+  %conv = trunc i64 %0 to i32
+  %conv1 = sext i32 %conv to i64
+  ret i64 %conv1
+}
+; CHECK: r0 = (s32)r1  # encoding: [0xbf,0x10,0x20,0x00,0x00,0x00,0x00,0x00]
+
+attributes #0 = { nounwind "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+
+!llvm.module.flags = !{!0, !1}
+!llvm.ident = !{!2}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 7, !"frame-pointer", i32 2}
+!2 = !{!"clang version 17.0.0 (https://github.com/llvm/llvm-project.git 569bd3b841e3167ddd7c6ceeddb282d3c280e761)"}
+!3 = !{!4, !4, i64 0}
+!4 = !{!"int", !5, i64 0}
+!5 = !{!"omnipotent char", !6, i64 0}
+!6 = !{!"Simple C/C++ TBAA"}
+!7 = !{!8, !8, i64 0}
+!8 = !{!"long", !5, i64 0}
Index: llvm/test/CodeGen/BPF/sext_ld.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/sext_ld.ll
@@ -0,0 +1,104 @@
+; RUN: llc -march=bpfel -mcpu=v4 -verify-machineinstrs -show-mc-encoding < %s | FileCheck %s
+; Source:
+;  int f1(char *p) {
+;return *p;
+; 

[PATCH] D143967: [DebugInfo][BPF] Add 'btf:type_tag' annotation in DWARF

2023-05-15 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

@dblaikie could you help take a look at this patch? Similar to 
https://reviews.llvm.org/D143966, this patch is the clang frontend change to 
support new btf_type_tag format, as we discussed and agreed with gcc community 
(https://lore.kernel.org/bpf/87r0w9jjoq@oracle.com/). Please let us know if 
you have any questions. Thanks!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D143967/new/

https://reviews.llvm.org/D143967

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150017: [Clang][BPF] Type print btf_type_tag properly

2023-05-08 Thread Yonghong Song via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG3060304906f0: [Clang][BPF] Type print btf_type_tag properly 
(authored by yonghong-song).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150017/new/

https://reviews.llvm.org/D150017

Files:
  clang/lib/AST/TypePrinter.cpp
  clang/test/AST/ast-dump-bpf-attr.c
  clang/test/Sema/attr-btf_type_tag.c
  clang/test/SemaCXX/sugar-common-types.cpp


Index: clang/test/SemaCXX/sugar-common-types.cpp
===
--- clang/test/SemaCXX/sugar-common-types.cpp
+++ clang/test/SemaCXX/sugar-common-types.cpp
@@ -95,7 +95,7 @@
 using SBTF3 = ::SS1 [[clang::btf_type_tag("2")]];
 
 N t21 = 0 ? (SBTF1){} : (SBTF3){}; // expected-error {{from 'SS1'}}
-N t22 = 0 ? (SBTF1){} : (SBTF2){}; // expected-error {{from 'SS1 
btf_type_tag(1)' (aka 'SS1')}}
+N t22 = 0 ? (SBTF1){} : (SBTF2){}; // expected-error {{from 'SS1 
__attribute__((btf_type_tag("1")))' (aka 'SS1')}}
 
 using QX = const SB1 *;
 using QY = const ::SB1 *;
Index: clang/test/Sema/attr-btf_type_tag.c
===
--- clang/test/Sema/attr-btf_type_tag.c
+++ clang/test/Sema/attr-btf_type_tag.c
@@ -28,8 +28,8 @@
 int g1 = _Generic((int *)0, int __tag1 *: 0);
 int g2 = _Generic((int __tag1 *)0, int *: 0);
 int g3 = _Generic(0,
-  int __tag1 * : 0, // expected-note {{compatible type 'int  
btf_type_tag(tag1)*' (aka 'int *') specified here}}
-  int * : 0, // expected-error {{type 'int *' in generic 
association compatible with previously specified type 'int  
btf_type_tag(tag1)*' (aka 'int *')}}
+  int __tag1 * : 0, // expected-note {{compatible type 'int  
__attribute__((btf_type_tag("tag1")))*' (aka 'int *') specified here}}
+  int * : 0, // expected-error {{type 'int *' in generic 
association compatible with previously specified type 'int  
__attribute__((btf_type_tag("tag1")))*' (aka 'int *')}}
   default : 0);
 
 // The btf_type_tag attribute will be ignored during overloadable type matching
Index: clang/test/AST/ast-dump-bpf-attr.c
===
--- /dev/null
+++ clang/test/AST/ast-dump-bpf-attr.c
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1 -triple bpf-pc-linux-gnu -ast-dump  %s \
+// RUN: | FileCheck --strict-whitespace %s
+
+int __attribute__((btf_type_tag("rcu"))) * g;
+// CHECK: VarDecl{{.*}}g 'int  __attribute__((btf_type_tag("rcu")))*'
Index: clang/lib/AST/TypePrinter.cpp
===
--- clang/lib/AST/TypePrinter.cpp
+++ clang/lib/AST/TypePrinter.cpp
@@ -1845,7 +1845,7 @@
 void TypePrinter::printBTFTagAttributedBefore(const BTFTagAttributedType *T,
   raw_ostream ) {
   printBefore(T->getWrappedType(), OS);
-  OS << " btf_type_tag(" << T->getAttr()->getBTFTypeTag() << ")";
+  OS << " __attribute__((btf_type_tag(\"" << T->getAttr()->getBTFTypeTag() << 
"\")))";
 }
 
 void TypePrinter::printBTFTagAttributedAfter(const BTFTagAttributedType *T,


Index: clang/test/SemaCXX/sugar-common-types.cpp
===
--- clang/test/SemaCXX/sugar-common-types.cpp
+++ clang/test/SemaCXX/sugar-common-types.cpp
@@ -95,7 +95,7 @@
 using SBTF3 = ::SS1 [[clang::btf_type_tag("2")]];
 
 N t21 = 0 ? (SBTF1){} : (SBTF3){}; // expected-error {{from 'SS1'}}
-N t22 = 0 ? (SBTF1){} : (SBTF2){}; // expected-error {{from 'SS1 btf_type_tag(1)' (aka 'SS1')}}
+N t22 = 0 ? (SBTF1){} : (SBTF2){}; // expected-error {{from 'SS1 __attribute__((btf_type_tag("1")))' (aka 'SS1')}}
 
 using QX = const SB1 *;
 using QY = const ::SB1 *;
Index: clang/test/Sema/attr-btf_type_tag.c
===
--- clang/test/Sema/attr-btf_type_tag.c
+++ clang/test/Sema/attr-btf_type_tag.c
@@ -28,8 +28,8 @@
 int g1 = _Generic((int *)0, int __tag1 *: 0);
 int g2 = _Generic((int __tag1 *)0, int *: 0);
 int g3 = _Generic(0,
-  int __tag1 * : 0, // expected-note {{compatible type 'int  btf_type_tag(tag1)*' (aka 'int *') specified here}}
-  int * : 0, // expected-error {{type 'int *' in generic association compatible with previously specified type 'int  btf_type_tag(tag1)*' (aka 'int *')}}
+  int __tag1 * : 0, // expected-note {{compatible type 'int  __attribute__((btf_type_tag("tag1")))*' (aka 'int *') specified here}}
+  int * : 0, // expected-error {{type 'int *' in generic association compatible with previously specified type 'int  __attribute__((btf_type_tag("tag1")))*' (aka 'int *')}}
   default : 0);
 
 // The btf_type_tag attribute will be ignored during overloadable type matching
Index: 

[PATCH] D150017: [Clang][BPF] Type print btf_type_tag properly

2023-05-08 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

In D150017#4326665 , @aaron.ballman 
wrote:

> LGTM, but please also add a release note about the fix.

Thanks @aaron.ballman. I intend to backport this patch to llvm16 and adding a 
release note about the fix will have backport conflict with llvm16, so I will 
add a release note separately after this is patch is merged into llvm17.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150017/new/

https://reviews.llvm.org/D150017

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150017: [Clang][BPF] Type print btf_type_tag properly

2023-05-05 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song created this revision.
yonghong-song added reviewers: ast, aaron.ballman.
Herald added a project: All.
yonghong-song requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

When running bcc tool execsnoop ([1]) which is built with latest llvm,
I hit the following error:

  $ sudo ./execsnoop.py
  /virtual/main.c:99:157: error: expected ')' 
data.ppid = ({ typeof(pid_t) _val; __builtin_memset(&_val, 0, 
sizeof(_val)); bpf_probe_read(&_val, sizeof(_val),
   (void *)&({ typeof(struct task_struct  btf_type_tag(rcu)*) _val; 
__builtin_memset(&_val, 0, sizeof(_val));
  ^   
bpf_probe_read(&_val, sizeof(_val), (void *)>real_parent); _val; 
})->tgid); _val; }); 

The failure reason is due to that the bcc rewriter printed type like

  struct task_struct  btf_type_tag(rcu)*

where the compiler cannot recognize what 'btf_type_tag(rcu)' is.

The above type is printed in [2] by UnaryOperator->getType().getAsString() 
(from clang)
in function ProbeVisitor::VisitUnaryOperator.

The original source type looks like ([3])

  struct task_struct {
... 
struct task_struct __rcu*real_parent;
... 
  }
  where '__rcu' is a macro expanding to '__attribute__((btf_type_tag("rcu")))'.

Let us print btf_type_tag properly in clang so bcc tools and broader type 
printing
will work properly.

With this patch, the above rewrited source code looks like

  data.ppid = ({ typeof(pid_t) _val; __builtin_memset(&_val, 0, sizeof(_val)); 
bpf_probe_read(&_val, sizeof(_val),
 (void *)&({ typeof(struct task_struct  
__attribute__((btf_type_tag("rcu")))*) _val; __builtin_memset(&_val, 0, 
sizeof(_val));
  bpf_probe_read(&_val, sizeof(_val), (void *)>real_parent); _val; 
})->tgid); _val; }); 

and execsnoop.py tool can run properly.

  [1] https://github.com/iovisor/bcc/blob/master/tools/exitsnoop.py
  [2] 
https://github.com/iovisor/bcc/blob/master/src/cc/frontends/clang/b_frontend_action.cc
  [3] https://github.com/torvalds/linux/blob/master/include/linux/sched.h


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D150017

Files:
  clang/lib/AST/TypePrinter.cpp
  clang/test/AST/ast-dump-bpf-attr.c
  clang/test/Sema/attr-btf_type_tag.c
  clang/test/SemaCXX/sugar-common-types.cpp


Index: clang/test/SemaCXX/sugar-common-types.cpp
===
--- clang/test/SemaCXX/sugar-common-types.cpp
+++ clang/test/SemaCXX/sugar-common-types.cpp
@@ -95,7 +95,7 @@
 using SBTF3 = ::SS1 [[clang::btf_type_tag("2")]];
 
 N t21 = 0 ? (SBTF1){} : (SBTF3){}; // expected-error {{from 'SS1'}}
-N t22 = 0 ? (SBTF1){} : (SBTF2){}; // expected-error {{from 'SS1 
btf_type_tag(1)' (aka 'SS1')}}
+N t22 = 0 ? (SBTF1){} : (SBTF2){}; // expected-error {{from 'SS1 
__attribute__((btf_type_tag("1")))' (aka 'SS1')}}
 
 using QX = const SB1 *;
 using QY = const ::SB1 *;
Index: clang/test/Sema/attr-btf_type_tag.c
===
--- clang/test/Sema/attr-btf_type_tag.c
+++ clang/test/Sema/attr-btf_type_tag.c
@@ -28,8 +28,8 @@
 int g1 = _Generic((int *)0, int __tag1 *: 0);
 int g2 = _Generic((int __tag1 *)0, int *: 0);
 int g3 = _Generic(0,
-  int __tag1 * : 0, // expected-note {{compatible type 'int  
btf_type_tag(tag1)*' (aka 'int *') specified here}}
-  int * : 0, // expected-error {{type 'int *' in generic 
association compatible with previously specified type 'int  
btf_type_tag(tag1)*' (aka 'int *')}}
+  int __tag1 * : 0, // expected-note {{compatible type 'int  
__attribute__((btf_type_tag("tag1")))*' (aka 'int *') specified here}}
+  int * : 0, // expected-error {{type 'int *' in generic 
association compatible with previously specified type 'int  
__attribute__((btf_type_tag("tag1")))*' (aka 'int *')}}
   default : 0);
 
 // The btf_type_tag attribute will be ignored during overloadable type matching
Index: clang/test/AST/ast-dump-bpf-attr.c
===
--- /dev/null
+++ clang/test/AST/ast-dump-bpf-attr.c
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1 -triple bpf-pc-linux-gnu -ast-dump  %s \
+// RUN: | FileCheck --strict-whitespace %s
+
+int __attribute__((btf_type_tag("rcu"))) * g;
+// CHECK: VarDecl{{.*}}g 'int  __attribute__((btf_type_tag("rcu")))*'
Index: clang/lib/AST/TypePrinter.cpp
===
--- clang/lib/AST/TypePrinter.cpp
+++ clang/lib/AST/TypePrinter.cpp
@@ -1845,7 +1845,7 @@
 void TypePrinter::printBTFTagAttributedBefore(const BTFTagAttributedType *T,
   raw_ostream ) {
   printBefore(T->getWrappedType(), OS);
-  OS << " btf_type_tag(" << T->getAttr()->getBTFTypeTag() << ")";
+  OS << " __attribute__((btf_type_tag(\"" << T->getAttr()->getBTFTypeTag() << 
"\")))";
 }
 
 void 

[PATCH] D143967: [DebugInfo][BPF] Add 'btf:type_tag' annotation in DWARF

2023-05-02 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

I see the following in the Summary:

  Type tag for CVR modifier type
  
  C:
  
  volatile int __attribute__((btf_type_tag("__b"))) b;
  
  DWARF:
  
  0x31:   DW_TAG_variable
DW_AT_name  ("b")
DW_AT_type  (0x3c "volatile int")
  
  0x3c:   DW_TAG_volatile_type
DW_AT_type  (0x45 "int")
  
  0x41: DW_TAG_LLVM_annotation
  DW_AT_name("btf:type_tag")
  DW_AT_const_value ("__b")

Basically, the btf_type_tag is put under 'volatile' type in dwarf. Is this what 
gcc also agrees with?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D143967/new/

https://reviews.llvm.org/D143967

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D143967: [DebugInfo][BPF] Add 'btf:type_tag' annotation in DWARF

2023-05-02 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

Related to `btf_type_is_modifier()` issue, Not that depending on call site, 
sometimes typedef is skipped and sometimes are not. So we could keep 
btf_type_is_modifier() as is and modify the call size to not skip typedef in 
specific places. Or we could remove typedef from the function and add a 'or' in 
call side if typedef needs to be skipped. The following are a few examples in 
kernel/bpf/btf.c:

/* Similar to btf_type_skip_modifiers() but does not skip typedefs. */
static const struct btf_type *btf_type_skip_qualifiers(const struct btf *btf,

  u32 id)

{

  const struct btf_type *t = btf_type_by_id(btf, id);
  
  while (btf_type_is_modifier(t) &&
 BTF_INFO_KIND(t->info) != BTF_KIND_TYPEDEF) {
  t = btf_type_by_id(btf, t->type);
  }  
 
  return t;

}

So we can add necessary changes (in specific context) to make the btf_type_tag 
approach with this patch work without breaking backward compatibility.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D143967/new/

https://reviews.llvm.org/D143967

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D144829: [WIP][BPF] Add a few new insns under cpu=v4

2023-02-26 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
yonghong-song requested review of this revision.
Herald added projects: clang, LLVM.
Herald added subscribers: llvm-commits, cfe-commits.

In [1], a few new insns are proposed to exgend BPF ISA to

  . fixing the limitation of existing insn (e.g., 16bit jmp offset)
  . adding new insns which may improve code quality (sext_ld, sext_mov, st) 
  . feature complete (sdiv, smod)
  . better user experience (bswap)

This patch implemented insn encoding for

  . sign-extended load
  . sign-extended mov 
  . sdiv/smod
  . bswap insns
  . unconditional jump with 32bit offset

The encoding for sign-extended load/mov, sdiv/smod are followed by
proposal in [1] except a couple of changes like sign-extended load
does not really have alu32 mode, and sign-extend mov only supports
`dst_reg<-(sign extend)src_reg` format. I will update the proposal
in bpf mailing list later.

The new bswap insns are generated under cpu=v4 for __builtin_bswap.
For cpu=v3 or earlier, for __builtin_bswap, be or le insns are generated
which is not intuitive for the user.

To support 32-bit branch offset, a 32-bit ja (JMPL) insn is implemented.
For conditional branch which is beyond 16-bit offset, llvm will do
some transformation 'cond_jmp' -> 'cond_jmp + jmpl' to simulate 32bit
conditional jmp. See BPFMIPeephole.cpp for details. The algorithm is
hueristic based. I have tested bpf selftest pyperf600 with unroll account
600 which can indeed generate 32-bit jump insn, e.g.,

  14:   06 00 00 00 27 ef 00 00 gotol +0xef27 


In the above, need to investigate why llvm-objdump prints out 
'', probably due to my introduction
of BPF specific fixup kinds.

Eduard is working on to add 'st' insn to cpu=v4.

This patch is in WIP stage since I need to:

  (1). investigate the above '' print,
  (2). add verifier support for the above insns.

References:

  [1] https://lore.kernel.org/bpf/01515302-c37d-2ee5-c950-2f556a4ca...@meta.com/


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D144829

Files:
  clang/lib/Basic/Targets/BPF.cpp
  clang/lib/Basic/Targets/BPF.h
  llvm/lib/Target/BPF/BPF.td
  llvm/lib/Target/BPF/BPFISelDAGToDAG.cpp
  llvm/lib/Target/BPF/BPFISelLowering.cpp
  llvm/lib/Target/BPF/BPFInstrFormats.td
  llvm/lib/Target/BPF/BPFInstrInfo.td
  llvm/lib/Target/BPF/BPFMIPeephole.cpp
  llvm/lib/Target/BPF/BPFSubtarget.cpp
  llvm/lib/Target/BPF/BPFSubtarget.h
  llvm/lib/Target/BPF/Disassembler/BPFDisassembler.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp
  llvm/lib/Target/BPF/MCTargetDesc/BPFMCFixups.h
  llvm/test/CodeGen/BPF/bswap.ll
  llvm/test/CodeGen/BPF/sdiv_smod.ll
  llvm/test/CodeGen/BPF/sext_ld.ll
  llvm/test/CodeGen/BPF/sext_mov.ll

Index: llvm/test/CodeGen/BPF/sext_mov.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/sext_mov.ll
@@ -0,0 +1,109 @@
+; RUN: llc -march=bpfel -mcpu=v4 -verify-machineinstrs -show-mc-encoding < %s | FileCheck %s
+; Source:
+;  short f1(int a) {
+;return (char)a;
+;  }
+;  int f2(int a) {
+;return (char)a;
+;  }
+;  long f3(int a) {
+;return (char)a;
+;  }
+;  int f4(int a) {
+;return (short)a;
+;  }
+;  long f5(int a) {
+;return (short)a;
+;  }
+;  long f6(long a) {
+;return (int)a;
+;  }
+; Compilation flags:
+;   clang -target bpf -O2 -S -emit-llvm -Xclang -disable-llvm-passes t.c
+
+; Function Attrs: nounwind
+define dso_local i16 @f1(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i8
+  %conv1 = sext i8 %conv to i16
+  ret i16 %conv1
+}
+; CHECK: w0 = (s8)w1  # encoding: [0xbc,0x10,0x08,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i32 @f2(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i8
+  %conv1 = sext i8 %conv to i32
+  ret i32 %conv1
+}
+; CHECK: w0 = (s8)w1  # encoding: [0xbc,0x10,0x08,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i64 @f3(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i8
+  %conv1 = sext i8 %conv to i64
+  ret i64 %conv1
+}
+; CHECK: r0 = (s8)r1  # encoding: [0xbf,0x10,0x08,0x00,0x00,0x00,0x00,0x00]
+
+; Function Attrs: nounwind
+define dso_local i32 @f4(i32 noundef %a) #0 {
+entry:
+  %a.addr = alloca i32, align 4
+  store i32 %a, ptr %a.addr, align 4, !tbaa !3
+  %0 = load i32, ptr %a.addr, align 4, !tbaa !3
+  %conv = trunc i32 %0 to i16
+  %conv1 = sext i16 %conv to i32
+  ret i32 %conv1
+}
+; CHECK: 

[PATCH] D142046: [BPF][clang] Ignore stack protector options for BPF target

2023-01-21 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

@dyung I just pushed the fix to the 'main' branch 
(https://github.com/llvm/llvm-project/commit/183d075055c591dedead7ece972f1bdea611aa3b).
 Please check it out. Thanks for reporting!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D142046/new/

https://reviews.llvm.org/D142046

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D142046: [BPF][clang] Ignore stack protector options for BPF target

2023-01-20 Thread Yonghong Song via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG56b038f887f3: [BPF][clang] Ignore stack protector options 
for BPF target (authored by eddyz87, committed by yonghong-song).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D142046/new/

https://reviews.llvm.org/D142046

Files:
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/test/CodeGen/bpf-stack-protector.c


Index: clang/test/CodeGen/bpf-stack-protector.c
===
--- /dev/null
+++ clang/test/CodeGen/bpf-stack-protector.c
@@ -0,0 +1,34 @@
+// REQUIRES: bpf-registered-target
+
+// RUN %clang -target bpf -S -emit-llvm -o - %s -fno-stack-protector 2>&1 \
+// RUN| FileCheck -check-prefix=OFF -check-prefix=COMMON %s
+
+// RUN: %clang -target bpf -S -emit-llvm -o - %s -fstack-protector 2>&1 \
+// RUN:| FileCheck -check-prefix=ON -check-prefix=COMMON %s
+
+// RUN: %clang -target bpf -S -emit-llvm -o - %s -fstack-protector-all 2>&1 \
+// RUN:| FileCheck -check-prefix=ALL -check-prefix=COMMON %s
+
+// RUN: %clang -target bpf -S -emit-llvm -o - %s -fstack-protector-strong 2>&1 
\
+// RUN:| FileCheck -check-prefix=STRONG -check-prefix=COMMON %s
+
+typedef __SIZE_TYPE__ size_t;
+
+int printf(const char * _Format, ...);
+size_t strlen(const char *s);
+char *strcpy(char *s1, const char *s2);
+
+// OFF-NOT: warning
+//  ON: warning: ignoring '-fstack-protector'
+// ALL: warning: ignoring '-fstack-protector-all'
+//  STRONG: warning: ignoring '-fstack-protector-strong'
+// COMMON-SAME: option as it is not currently supported for target 'bpf'
+
+// COMMON: define {{.*}}void @test1(ptr noundef %msg) #[[A:.*]] {
+void test1(const char *msg) {
+  char a[strlen(msg) + 1];
+  strcpy(a, msg);
+  printf("%s\n", a);
+}
+
+// COMMON-NOT: attributes #[[A]] = {{.*}} ssp
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -3239,6 +3239,12 @@
   StackProtectorLevel = LangOptions::SSPStrong;
 else if (A->getOption().matches(options::OPT_fstack_protector_all))
   StackProtectorLevel = LangOptions::SSPReq;
+
+if (EffectiveTriple.isBPF() && StackProtectorLevel != LangOptions::SSPOff) 
{
+  D.Diag(diag::warn_drv_unsupported_option_for_target)
+  << A->getSpelling() << EffectiveTriple.getTriple();
+  StackProtectorLevel = DefaultStackProtectorLevel;
+}
   } else {
 StackProtectorLevel = DefaultStackProtectorLevel;
   }


Index: clang/test/CodeGen/bpf-stack-protector.c
===
--- /dev/null
+++ clang/test/CodeGen/bpf-stack-protector.c
@@ -0,0 +1,34 @@
+// REQUIRES: bpf-registered-target
+
+// RUN %clang -target bpf -S -emit-llvm -o - %s -fno-stack-protector 2>&1 \
+// RUN| FileCheck -check-prefix=OFF -check-prefix=COMMON %s
+
+// RUN: %clang -target bpf -S -emit-llvm -o - %s -fstack-protector 2>&1 \
+// RUN:| FileCheck -check-prefix=ON -check-prefix=COMMON %s
+
+// RUN: %clang -target bpf -S -emit-llvm -o - %s -fstack-protector-all 2>&1 \
+// RUN:| FileCheck -check-prefix=ALL -check-prefix=COMMON %s
+
+// RUN: %clang -target bpf -S -emit-llvm -o - %s -fstack-protector-strong 2>&1 \
+// RUN:| FileCheck -check-prefix=STRONG -check-prefix=COMMON %s
+
+typedef __SIZE_TYPE__ size_t;
+
+int printf(const char * _Format, ...);
+size_t strlen(const char *s);
+char *strcpy(char *s1, const char *s2);
+
+// OFF-NOT: warning
+//  ON: warning: ignoring '-fstack-protector'
+// ALL: warning: ignoring '-fstack-protector-all'
+//  STRONG: warning: ignoring '-fstack-protector-strong'
+// COMMON-SAME: option as it is not currently supported for target 'bpf'
+
+// COMMON: define {{.*}}void @test1(ptr noundef %msg) #[[A:.*]] {
+void test1(const char *msg) {
+  char a[strlen(msg) + 1];
+  strcpy(a, msg);
+  printf("%s\n", a);
+}
+
+// COMMON-NOT: attributes #[[A]] = {{.*}} ssp
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -3239,6 +3239,12 @@
   StackProtectorLevel = LangOptions::SSPStrong;
 else if (A->getOption().matches(options::OPT_fstack_protector_all))
   StackProtectorLevel = LangOptions::SSPReq;
+
+if (EffectiveTriple.isBPF() && StackProtectorLevel != LangOptions::SSPOff) {
+  D.Diag(diag::warn_drv_unsupported_option_for_target)
+  << A->getSpelling() << EffectiveTriple.getTriple();
+  StackProtectorLevel = DefaultStackProtectorLevel;
+}
   } else {
 StackProtectorLevel = DefaultStackProtectorLevel;
   }
___
cfe-commits 

[PATCH] D142046: [BPF][clang] Ignore stack protector options for BPF target

2023-01-19 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

Sorry, I double checked. '-fstack-protector -fno-stack-protector' will not 
result in warnings. So the patch LGTM. So gentoo people can add 
-fno-stack-protector to suppress warnings.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D142046/new/

https://reviews.llvm.org/D142046

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D142046: [BPF][clang] Ignore stack protector options for BPF target

2023-01-19 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

@ast With this patch, gentoo clang compilation will hit the warning even if 
people appends -fno-stack-protector. Is this okay? In general, if the option is 
'-fstack-protector -fno-stack-protector', we should not issue warning, right?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D142046/new/

https://reviews.llvm.org/D142046

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D142046: [BPF][clang] Ignore stack protector options for BPF target

2023-01-18 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

@compnerd  could you also take a look at this patch?

First, some background about this patch. The reason of this patch is due to:

  
https://lore.kernel.org/bpf/CAOFdcFPnHEc2qd-=c+hdk4ntjjfbhsf4r-g7pdjtrbat6mu...@mail.gmail.com/

Further the following link has details about hardened gentoo and modified clang 
default flags:

  https://wiki.gentoo.org/wiki/Hardened_Gentoo

Unfortunately, we don't want -fstack-protector for bpf target as bpf kernel 
verifier will do stack verification.
This patch tries to ignore -fstack-protector. NVPTX arch has the same need of 
ignoring -fstack-protector.
This patch presented a different message than NVPTX. This patch:

  clang-16: warning: ignoring '-fstack-protector' option as it is not currently 
supported for target 'bpf' [-Woption-ignored]

NVPTX

  clang-16: warning: argument unused during compilation: '-fstack-protector' 
[-Wunused-command-line-argument]

Does clang community has a preference for the above two formats?

Let us say we do implement this patch and merged into clang, then using gentoo 
clang compiler, we will hit
the warning:

  clang-16: warning: ignoring '-fstack-protector' option as it is not currently 
supported for target 'bpf' [-Woption-ignored]

I suspect gentoo community might complain. I don't think we should add 
-Wno-option-ignored since this may cause
unintended issues with compiler command line arguments. 
Without -Wno-option-ignored, gentoo community might wants to append 
-fno-stack-protector to disable stack-protector,
but with the current implementation, we might actually trigger another warning.

So we have to two choices here for bpf target:

  (1). ignore any stack-protector option and do not issue any warning, or
  (2). append -fno-stack-protector by clang to compilation flags.

We can document this behavior in related clang (and kernel) docs.

WDYT?

WDYT?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D142046/new/

https://reviews.llvm.org/D142046

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D141424: [clang][Sema] Fix uninitialized `SourceLocation` for types with multiple attributes and macros.

2023-01-17 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song accepted this revision.
yonghong-song added a comment.
This revision is now accepted and ready to land.

LGTM. Thanks!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141424/new/

https://reviews.llvm.org/D141424

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D140970: [BPF] preserve btf_decl_tag for parameters of extern functions

2023-01-06 Thread Yonghong Song via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG58bdf8f9a8c3: [BPF] preserve btf_decl_tag for parameters of 
extern functions (authored by eddyz87, committed by yonghong-song).

Changed prior to commit:
  https://reviews.llvm.org/D140970?vs=486295=487054#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D140970/new/

https://reviews.llvm.org/D140970

Files:
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/test/CodeGen/bpf-decl-tag-extern-func-args.c


Index: clang/test/CodeGen/bpf-decl-tag-extern-func-args.c
===
--- /dev/null
+++ clang/test/CodeGen/bpf-decl-tag-extern-func-args.c
@@ -0,0 +1,38 @@
+// REQUIRES: bpf-registered-target
+// RUN: %clang -target bpf -S -emit-llvm -g -O2 -o - %s  | FileCheck %s
+
+#define __decl_tag(x) __attribute__((btf_decl_tag(x)))
+
+extern void foo(int aa __decl_tag("aa_tag"), long bb __decl_tag("bb_tag"));
+extern void bar(char cc __decl_tag("cc_tag"));
+extern void buz(int __decl_tag("buz_arg_tag"));
+
+void root(void) {
+  foo(0, 1);
+  bar(0);
+  buz(0);
+}
+// CHECK: [[foo:![0-9]+]] = !DISubprogram(name: "foo", {{.*}}, retainedNodes: 
[[foo_nodes:![0-9]+]])
+// CHECK: [[foo_nodes]] = !{[[aa:![0-9]+]], [[bb:![0-9]+]]}
+
+// CHECK: [[aa]] = !DILocalVariable(name: "aa", arg: 1, scope: [[foo]], 
{{.*}}, annotations: [[aa_annot:![0-9]+]])
+// CHECK: [[aa_annot]] = !{[[aa_tag:![0-9]+]]}
+// CHECK: [[aa_tag]] = !{!"btf_decl_tag", !"aa_tag"}
+
+// CHECK: [[bb]] = !DILocalVariable(name: "bb", arg: 2, scope: [[foo]], 
{{.*}}, annotations: [[bb_annot:![0-9]+]])
+// CHECK: [[bb_annot]] = !{[[bb_tag:![0-9]+]]}
+// CHECK: [[bb_tag]] = !{!"btf_decl_tag", !"bb_tag"}
+
+// CHECK: [[bar:![0-9]+]] = !DISubprogram(name: "bar", {{.*}}, retainedNodes: 
[[bar_nodes:![0-9]+]])
+// CHECK: [[bar_nodes]] = !{[[cc:![0-9]+]]}
+
+// CHECK: [[cc]] = !DILocalVariable(name: "cc", arg: 1, scope: [[bar]], 
{{.*}}, annotations: [[cc_annot:![0-9]+]])
+// CHECK: [[cc_annot]] = !{[[cc_tag:![0-9]+]]}
+// CHECK: [[cc_tag]] = !{!"btf_decl_tag", !"cc_tag"}
+
+// CHECK: [[buz:![0-9]+]] = !DISubprogram(name: "buz", {{.*}}, retainedNodes: 
[[buz_nodes:![0-9]+]])
+// CHECK: [[buz_nodes]] = !{[[buz_arg:![0-9]+]]}
+
+// CHECK: [[buz_arg]] = !DILocalVariable(arg: 1, scope: [[buz]], {{.*}}, 
annotations: [[buz_arg_annot:![0-9]+]])
+// CHECK: [[buz_arg_annot]] = !{[[buz_arg_tag:![0-9]+]]}
+// CHECK: [[buz_arg_tag]] = !{!"btf_decl_tag", !"buz_arg_tag"}
Index: clang/lib/CodeGen/CGDebugInfo.cpp
===
--- clang/lib/CodeGen/CGDebugInfo.cpp
+++ clang/lib/CodeGen/CGDebugInfo.cpp
@@ -4206,10 +4206,28 @@
 SPFlags |= llvm::DISubprogram::SPFlagOptimized;
 
   llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(D);
-  llvm::DISubprogram *SP = DBuilder.createFunction(
-  FDContext, Name, LinkageName, Unit, LineNo,
-  getOrCreateFunctionType(D, FnType, Unit), ScopeLine, Flags, SPFlags,
-  TParamsArray.get(), getFunctionDeclaration(D), nullptr, Annotations);
+  llvm::DISubroutineType *STy = getOrCreateFunctionType(D, FnType, Unit);
+  llvm::DISubprogram *SP =
+  DBuilder.createFunction(FDContext, Name, LinkageName, Unit, LineNo, STy,
+  ScopeLine, Flags, SPFlags, TParamsArray.get(),
+  getFunctionDeclaration(D), nullptr, Annotations);
+
+  // Preserve btf_decl_tag attributes for parameters of extern functions
+  // for BPF target. The parameters created in this loop are attached as
+  // DISubprogram's retainedNodes in the subsequent finalizeSubprogram call.
+  if (IsDeclForCallSite && CGM.getTarget().getTriple().isBPF()) {
+if (auto *FD = dyn_cast(D)) {
+  llvm::DITypeRefArray ParamTypes = STy->getTypeArray();
+  unsigned ArgNo = 1;
+  for (ParmVarDecl *PD : FD->parameters()) {
+llvm::DINodeArray ParamAnnotations = CollectBTFDeclTagAnnotations(PD);
+DBuilder.createParameterVariable(
+SP, PD->getName(), ArgNo, Unit, LineNo, ParamTypes[ArgNo], true,
+llvm::DINode::FlagZero, ParamAnnotations);
+++ArgNo;
+  }
+}
+  }
 
   if (IsDeclForCallSite)
 Fn->setSubprogram(SP);


Index: clang/test/CodeGen/bpf-decl-tag-extern-func-args.c
===
--- /dev/null
+++ clang/test/CodeGen/bpf-decl-tag-extern-func-args.c
@@ -0,0 +1,38 @@
+// REQUIRES: bpf-registered-target
+// RUN: %clang -target bpf -S -emit-llvm -g -O2 -o - %s  | FileCheck %s
+
+#define __decl_tag(x) __attribute__((btf_decl_tag(x)))
+
+extern void foo(int aa __decl_tag("aa_tag"), long bb __decl_tag("bb_tag"));
+extern void bar(char cc __decl_tag("cc_tag"));
+extern void buz(int __decl_tag("buz_arg_tag"));
+
+void root(void) {
+  foo(0, 1);
+  bar(0);
+  buz(0);
+}
+// CHECK: [[foo:![0-9]+]] = !DISubprogram(name: "foo", {{.*}}, retainedNodes: 

[PATCH] D140970: [BPF] preserve btf_decl_tag for parameters of extern functions

2023-01-06 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song accepted this revision.
yonghong-song added a comment.
This revision is now accepted and ready to land.

LGTM. I will add `// REQUIRES: bpf-registered-target` in the test to ensure it 
only executed with bpf target.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D140970/new/

https://reviews.llvm.org/D140970

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D140929: [BPF] Support for "btf_decl_tag" annotations for arguments of extern functions

2023-01-03 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added inline comments.



Comment at: clang/lib/CodeGen/CGDebugInfo.cpp:4218
+  // DISubprogram's retainedNodes in the subsequent finalizeSubprogram call.
+  if (IsDeclForCallSite && CGM.getTarget().getTriple().isBPF()) {
+if (auto *FD = dyn_cast(D)) {

yonghong-song wrote:
> Looks like this is for bpf only? Can we extend it to non-bpf as well? 
> Currently, I think most use case, if any, will be in vmlinux (e.g., for 
> struct_ops callback, etc).
sorry, the above comment is not right about struct_ops, I thought this is for 
function pointer parameters, but it is for extern functions. Yes, for vmlinux, 
we don't need to support extern func arg decl tags since we always have real 
function definitions. So bpf only support is okay.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D140929/new/

https://reviews.llvm.org/D140929

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D140929: [BPF] Support for "btf_decl_tag" annotations for arguments of extern functions

2023-01-03 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

Please separate the patch to clang and llvm part.




Comment at: clang/lib/CodeGen/CGDebugInfo.cpp:4218
+  // DISubprogram's retainedNodes in the subsequent finalizeSubprogram call.
+  if (IsDeclForCallSite && CGM.getTarget().getTriple().isBPF()) {
+if (auto *FD = dyn_cast(D)) {

Looks like this is for bpf only? Can we extend it to non-bpf as well? 
Currently, I think most use case, if any, will be in vmlinux (e.g., for 
struct_ops callback, etc).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D140929/new/

https://reviews.llvm.org/D140929

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D136807: [clang][Sema] Fix a clang crash with btf_type_tag

2022-11-01 Thread Yonghong Song via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG2f047b56ba0e: [clang][Sema] Fix a clang crash with 
btf_type_tag (authored by yonghong-song).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D136807/new/

https://reviews.llvm.org/D136807

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaType.cpp
  clang/test/CodeGen/attr-btf_type_tag-func-ptr.c


Index: clang/test/CodeGen/attr-btf_type_tag-func-ptr.c
===
--- /dev/null
+++ clang/test/CodeGen/attr-btf_type_tag-func-ptr.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -debug-info-kind=limited -S 
-emit-llvm -o - %s | FileCheck %s
+
+struct t {
+ int (__attribute__((btf_type_tag("rcu"))) *f)();
+ int a;
+};
+int foo(struct t *arg) {
+  return arg->a;
+}
+
+// CHECK:  !DIDerivedType(tag: DW_TAG_member, name: "f"
+// CHECK-SAME: baseType: ![[L18:[0-9]+]]
+// CHECK:  ![[L18]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: 
![[#]], size: [[#]], annotations: ![[L21:[0-9]+]])
+// CHECK:  ![[L21]] = !{![[L22:[0-9]+]]}
+// CHECK:  ![[L22]] = !{!"btf_type_tag", !"rcu"}
Index: clang/lib/Sema/SemaType.cpp
===
--- clang/lib/Sema/SemaType.cpp
+++ clang/lib/Sema/SemaType.cpp
@@ -6515,6 +6515,9 @@
   CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
 }
 
+while (BTFTagAttributedTypeLoc TL = 
CurrTL.getAs())
+  CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
+
 while (DependentAddressSpaceTypeLoc TL =
CurrTL.getAs()) {
   fillDependentAddressSpaceTypeLoc(TL, D.getTypeObject(i).getAttrs());
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -268,6 +268,8 @@
   functions. `Issue 56154 `_
 - Fix handling of unexpanded packs in template argument expressions.
   `Issue 58679 `_
+- Fix a crash when a ``btf_type_tag`` attribute is applied to the pointee of
+  a function pointer.
 
 Improvements to Clang's diagnostics
 ^^^


Index: clang/test/CodeGen/attr-btf_type_tag-func-ptr.c
===
--- /dev/null
+++ clang/test/CodeGen/attr-btf_type_tag-func-ptr.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -debug-info-kind=limited -S -emit-llvm -o - %s | FileCheck %s
+
+struct t {
+ int (__attribute__((btf_type_tag("rcu"))) *f)();
+ int a;
+};
+int foo(struct t *arg) {
+  return arg->a;
+}
+
+// CHECK:  !DIDerivedType(tag: DW_TAG_member, name: "f"
+// CHECK-SAME: baseType: ![[L18:[0-9]+]]
+// CHECK:  ![[L18]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[#]], size: [[#]], annotations: ![[L21:[0-9]+]])
+// CHECK:  ![[L21]] = !{![[L22:[0-9]+]]}
+// CHECK:  ![[L22]] = !{!"btf_type_tag", !"rcu"}
Index: clang/lib/Sema/SemaType.cpp
===
--- clang/lib/Sema/SemaType.cpp
+++ clang/lib/Sema/SemaType.cpp
@@ -6515,6 +6515,9 @@
   CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
 }
 
+while (BTFTagAttributedTypeLoc TL = CurrTL.getAs())
+  CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
+
 while (DependentAddressSpaceTypeLoc TL =
CurrTL.getAs()) {
   fillDependentAddressSpaceTypeLoc(TL, D.getTypeObject(i).getAttrs());
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -268,6 +268,8 @@
   functions. `Issue 56154 `_
 - Fix handling of unexpanded packs in template argument expressions.
   `Issue 58679 `_
+- Fix a crash when a ``btf_type_tag`` attribute is applied to the pointee of
+  a function pointer.
 
 Improvements to Clang's diagnostics
 ^^^
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D136807: [clang][Sema] Fix a clang crash with btf_type_tag

2022-11-01 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 472305.
yonghong-song added a comment.

- improve the bug description in release note.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D136807/new/

https://reviews.llvm.org/D136807

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaType.cpp
  clang/test/CodeGen/attr-btf_type_tag-func-ptr.c


Index: clang/test/CodeGen/attr-btf_type_tag-func-ptr.c
===
--- /dev/null
+++ clang/test/CodeGen/attr-btf_type_tag-func-ptr.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -debug-info-kind=limited -S 
-emit-llvm -o - %s | FileCheck %s
+
+struct t {
+ int (__attribute__((btf_type_tag("rcu"))) *f)();
+ int a;
+};
+int foo(struct t *arg) {
+  return arg->a;
+}
+
+// CHECK:  !DIDerivedType(tag: DW_TAG_member, name: "f"
+// CHECK-SAME: baseType: ![[L18:[0-9]+]]
+// CHECK:  ![[L18]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: 
![[#]], size: [[#]], annotations: ![[L21:[0-9]+]])
+// CHECK:  ![[L21]] = !{![[L22:[0-9]+]]}
+// CHECK:  ![[L22]] = !{!"btf_type_tag", !"rcu"}
Index: clang/lib/Sema/SemaType.cpp
===
--- clang/lib/Sema/SemaType.cpp
+++ clang/lib/Sema/SemaType.cpp
@@ -6515,6 +6515,9 @@
   CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
 }
 
+while (BTFTagAttributedTypeLoc TL = 
CurrTL.getAs())
+  CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
+
 while (DependentAddressSpaceTypeLoc TL =
CurrTL.getAs()) {
   fillDependentAddressSpaceTypeLoc(TL, D.getTypeObject(i).getAttrs());
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -268,6 +268,8 @@
   functions. `Issue 56154 `_
 - Fix handling of unexpanded packs in template argument expressions.
   `Issue 58679 `_
+- Fix a crash when a ``btf_type_tag`` attribute is applied to the pointee of
+  a function pointer.
 
 Improvements to Clang's diagnostics
 ^^^


Index: clang/test/CodeGen/attr-btf_type_tag-func-ptr.c
===
--- /dev/null
+++ clang/test/CodeGen/attr-btf_type_tag-func-ptr.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -debug-info-kind=limited -S -emit-llvm -o - %s | FileCheck %s
+
+struct t {
+ int (__attribute__((btf_type_tag("rcu"))) *f)();
+ int a;
+};
+int foo(struct t *arg) {
+  return arg->a;
+}
+
+// CHECK:  !DIDerivedType(tag: DW_TAG_member, name: "f"
+// CHECK-SAME: baseType: ![[L18:[0-9]+]]
+// CHECK:  ![[L18]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[#]], size: [[#]], annotations: ![[L21:[0-9]+]])
+// CHECK:  ![[L21]] = !{![[L22:[0-9]+]]}
+// CHECK:  ![[L22]] = !{!"btf_type_tag", !"rcu"}
Index: clang/lib/Sema/SemaType.cpp
===
--- clang/lib/Sema/SemaType.cpp
+++ clang/lib/Sema/SemaType.cpp
@@ -6515,6 +6515,9 @@
   CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
 }
 
+while (BTFTagAttributedTypeLoc TL = CurrTL.getAs())
+  CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
+
 while (DependentAddressSpaceTypeLoc TL =
CurrTL.getAs()) {
   fillDependentAddressSpaceTypeLoc(TL, D.getTypeObject(i).getAttrs());
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -268,6 +268,8 @@
   functions. `Issue 56154 `_
 - Fix handling of unexpanded packs in template argument expressions.
   `Issue 58679 `_
+- Fix a crash when a ``btf_type_tag`` attribute is applied to the pointee of
+  a function pointer.
 
 Improvements to Clang's diagnostics
 ^^^
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D136807: [clang][Sema] Fix a clang crash with btf_type_tag

2022-10-31 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

@aaron.ballman or @dblaikie I have addressed the comments, could you take a 
look again?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D136807/new/

https://reviews.llvm.org/D136807

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D136041: [clang][DebugInfo] Emit DISubprogram for extern functions with reserved names

2022-10-28 Thread Yonghong Song via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG524c640090a8: [clang][DebugInfo] Emit DISubprogram for 
extern functions with reserved names (authored by eddyz87, committed by 
yonghong-song).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D136041/new/

https://reviews.llvm.org/D136041

Files:
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/test/CodeGen/debug-info-extern-call.c
  llvm/lib/IR/Verifier.cpp
  llvm/test/Verifier/callsite-dbgloc.ll


Index: llvm/test/Verifier/callsite-dbgloc.ll
===
--- llvm/test/Verifier/callsite-dbgloc.ll
+++ llvm/test/Verifier/callsite-dbgloc.ll
@@ -33,18 +33,24 @@
   ret void, !dbg !22
 }
 
-declare void @i(...) #1
+declare !dbg !23 void @i(...) #1
 
 ; Function Attrs: nounwind ssp uwtable
 define void @g() #0 !dbg !11 {
 entry:
 ; Manually removed !dbg.
 ; CHECK: inlinable function call in a function with debug info must have a 
!dbg location
+; CHECK: @h()
   call void @h()
 ; CHECK-NOT: inlinable function call in a function with debug info must have a 
!dbg location
+; CHECK-NOT: @j()
   call void @j()
 ; CHECK: inlinable function call in a function with debug info must have a 
!dbg location
+; CHECK: @k()
   call void @k()
+; CHECK-NOT: inlinable function call in a function with debug info must have a 
!dbg location
+; CHECK-NOT: @i()
+  call void (...) @i()
   ret void, !dbg !13
 }
 
@@ -86,3 +92,6 @@
 !20 = distinct !DISubprogram(name: "k", scope: !1, file: !1, line: 6, type: 
!8, isLocal: false, isDefinition: true, scopeLine: 6, isOptimized: false, unit: 
!0, retainedNodes: !2)
 !21 = !DILocation(line: 4, column: 12, scope: !20)
 !22 = !DILocation(line: 4, column: 17, scope: !20)
+!23 = !DISubprogram(name: "i", scope: !1, file: !1, line: 1, type: !24, flags: 
DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !2)
+!24 = !DISubroutineType(types: !25)
+!25 = !{null}
Index: llvm/lib/IR/Verifier.cpp
===
--- llvm/lib/IR/Verifier.cpp
+++ llvm/lib/IR/Verifier.cpp
@@ -3468,9 +3468,11 @@
   // Verify that each inlinable callsite of a debug-info-bearing function in a
   // debug-info-bearing function has a debug location attached to it. Failure 
to
   // do so causes assertion failures when the inliner sets up inline scope info
-  // (Interposable functions are not inlinable).
+  // (Interposable functions are not inlinable, neither are functions without
+  //  definitions.)
   if (Call.getFunction()->getSubprogram() && Call.getCalledFunction() &&
   !Call.getCalledFunction()->isInterposable() &&
+  !Call.getCalledFunction()->isDeclaration() &&
   Call.getCalledFunction()->getSubprogram())
 CheckDI(Call.getDebugLoc(),
 "inlinable function call in a function with "
Index: clang/test/CodeGen/debug-info-extern-call.c
===
--- clang/test/CodeGen/debug-info-extern-call.c
+++ clang/test/CodeGen/debug-info-extern-call.c
@@ -29,8 +29,8 @@
 // DECLS-FOR-EXTERN: [[FN1_TYPES]] = !{[[X_TYPE:![0-9]+]],
 // DECLS-FOR-EXTERN: [[X_TYPE]] = !DIDerivedType(tag: DW_TAG_typedef, name: 
"x",
 // DECLS-FOR-EXTERN-SAME: baseType: [[INT_TYPE]])
-// DECLS-FOR-EXTERN-NOT: !DISubprogram(name: "memcmp"
-// DECLS-FOR-EXTERN-NOT: !DISubprogram(name: "__some_reserved_name"
+// DECLS-FOR-EXTERN: !DISubprogram(name: "memcmp"
+// DECLS-FOR-EXTERN: !DISubprogram(name: "__some_reserved_name"
 
 // NO-DECLS-FOR-EXTERN-NOT: !DISubprogram(name: "fn1"
 // NO-DECLS-FOR-EXTERN-NOT: !DISubprogram(name: "memcmp"
Index: clang/lib/CodeGen/CGDebugInfo.cpp
===
--- clang/lib/CodeGen/CGDebugInfo.cpp
+++ clang/lib/CodeGen/CGDebugInfo.cpp
@@ -4228,17 +4228,11 @@
   if (Func->getSubprogram())
 return;
 
-  // Do not emit a declaration subprogram for a builtin, a function with 
nodebug
-  // attribute, or if call site info isn't required. Also, elide declarations
-  // for functions with reserved names, as call site-related features aren't
-  // interesting in this case (& also, the compiler may emit calls to these
-  // functions without debug locations, which makes the verifier complain).
-  if (CalleeDecl->getBuiltinID() != 0 || CalleeDecl->hasAttr() ||
+  // Do not emit a declaration subprogram for a function with nodebug
+  // attribute, or if call site info isn't required.
+  if (CalleeDecl->hasAttr() ||
   getCallSiteRelatedAttrs() == llvm::DINode::FlagZero)
 return;
-  if (CalleeDecl->isReserved(CGM.getLangOpts()) !=
-  ReservedIdentifierStatus::NotReserved)
-return;
 
   // If there is no DISubprogram attached to the function being called,
   // create the one describing the function in order to have complete


Index: llvm/test/Verifier/callsite-dbgloc.ll
===
--- 

[PATCH] D136807: [clang][Sema] Fix a clang crash with btf_type_tag

2022-10-27 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 471216.
yonghong-song added a comment.

- Add the bug fix description in Release Note
- Actually compare debuginfo btf_type_tag output instead of marking 
'expected-no-diagnostics'.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D136807/new/

https://reviews.llvm.org/D136807

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaType.cpp
  clang/test/CodeGen/attr-btf_type_tag-func-ptr.c


Index: clang/test/CodeGen/attr-btf_type_tag-func-ptr.c
===
--- /dev/null
+++ clang/test/CodeGen/attr-btf_type_tag-func-ptr.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -debug-info-kind=limited -S 
-emit-llvm -o - %s | FileCheck %s
+
+struct t {
+ int (__attribute__((btf_type_tag("rcu"))) *f)();
+ int a;
+};
+int foo(struct t *arg) {
+  return arg->a;
+}
+
+// CHECK:  !DIDerivedType(tag: DW_TAG_member, name: "f"
+// CHECK-SAME: baseType: ![[L18:[0-9]+]]
+// CHECK:  ![[L18]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: 
![[#]], size: [[#]], annotations: ![[L21:[0-9]+]])
+// CHECK:  ![[L21]] = !{![[L22:[0-9]+]]}
+// CHECK:  ![[L22]] = !{!"btf_type_tag", !"rcu"}
Index: clang/lib/Sema/SemaType.cpp
===
--- clang/lib/Sema/SemaType.cpp
+++ clang/lib/Sema/SemaType.cpp
@@ -6515,6 +6515,9 @@
   CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
 }
 
+while (BTFTagAttributedTypeLoc TL = 
CurrTL.getAs())
+  CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
+
 while (DependentAddressSpaceTypeLoc TL =
CurrTL.getAs()) {
   fillDependentAddressSpaceTypeLoc(TL, D.getTypeObject(i).getAttrs());
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -258,6 +258,8 @@
 - Fix template arguments of pointer and reference not taking the type as
   part of their identity.
   `Issue 47136 `_
+- Fix a crash when btf_type_tag attr is applied to the pointee of
+  a function pointer.
 
 Improvements to Clang's diagnostics
 ^^^


Index: clang/test/CodeGen/attr-btf_type_tag-func-ptr.c
===
--- /dev/null
+++ clang/test/CodeGen/attr-btf_type_tag-func-ptr.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -debug-info-kind=limited -S -emit-llvm -o - %s | FileCheck %s
+
+struct t {
+ int (__attribute__((btf_type_tag("rcu"))) *f)();
+ int a;
+};
+int foo(struct t *arg) {
+  return arg->a;
+}
+
+// CHECK:  !DIDerivedType(tag: DW_TAG_member, name: "f"
+// CHECK-SAME: baseType: ![[L18:[0-9]+]]
+// CHECK:  ![[L18]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[#]], size: [[#]], annotations: ![[L21:[0-9]+]])
+// CHECK:  ![[L21]] = !{![[L22:[0-9]+]]}
+// CHECK:  ![[L22]] = !{!"btf_type_tag", !"rcu"}
Index: clang/lib/Sema/SemaType.cpp
===
--- clang/lib/Sema/SemaType.cpp
+++ clang/lib/Sema/SemaType.cpp
@@ -6515,6 +6515,9 @@
   CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
 }
 
+while (BTFTagAttributedTypeLoc TL = CurrTL.getAs())
+  CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
+
 while (DependentAddressSpaceTypeLoc TL =
CurrTL.getAs()) {
   fillDependentAddressSpaceTypeLoc(TL, D.getTypeObject(i).getAttrs());
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -258,6 +258,8 @@
 - Fix template arguments of pointer and reference not taking the type as
   part of their identity.
   `Issue 47136 `_
+- Fix a crash when btf_type_tag attr is applied to the pointee of
+  a function pointer.
 
 Improvements to Clang's diagnostics
 ^^^
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D136807: [clang][Sema] Fix a clang crash with btf_type_tag

2022-10-27 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

In D136807#3888317 , @aaron.ballman 
wrote:

> The changes generally look good to me, but can you add a release note for the 
> fix as well?

Yes, will add the bug fix brief description to clang/docs/ReleaseNotes.rst




Comment at: clang/test/Sema/attr-btf_type_tag-func-ptr.c:1
+// RUN: %clang_cc1 -x c -triple x86_64-pc-linux-gnu -dwarf-version=4 
-fsyntax-only -verify %s
+

aaron.ballman wrote:
> Does the dwarf version actually matter or can that be removed as well?
I will move the test to CodeGen directory and actually check dwarf output for 
btf_type_tag in debuginfo which will address David's suggestion as well.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D136807/new/

https://reviews.llvm.org/D136807

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D136807: [clang][Sema] Fix a clang crash with btf_type_tag

2022-10-26 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song created this revision.
yonghong-song added reviewers: aaron.ballman, dblaikie.
yonghong-song added a project: clang.
Herald added a project: All.
yonghong-song requested review of this revision.
Herald added a subscriber: cfe-commits.

For the following program,

  $ cat t.c 
  struct t { 
   int (__attribute__((btf_type_tag("rcu"))) *f)();
   int a;
  };  
  int foo(struct t *arg) {
return arg->a;
  }

Compiling with 'clang -g -O2 -S t.c' will cause a failure like below:

  clang: /home/yhs/work/llvm-project/clang/lib/Sema/SemaType.cpp:6391: void 
{anonymous}::DeclaratorLocFiller::VisitParenTypeLoc(clang::ParenTypeLoc):
 Assertion `Chunk.Kind == DeclaratorChunk::Paren' failed.
  PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ 
and include the crash backtrace, preprocessed source, and associated run script.
  Stack dump:
  ..
  #5 0x7f89e4280ea5 abort (/lib64/libc.so.6+0x21ea5)
  #6 0x7f89e4280d79 _nl_load_domain.cold.0 (/lib64/libc.so.6+0x21d79)
  #7 0x7f89e42a6456 (/lib64/libc.so.6+0x47456)
  #8 0x045c2596 GetTypeSourceInfoForDeclarator((anonymous 
namespace)::TypeProcessingState&, clang::QualType, clang::TypeSourceInfo*) 
SemaType.cpp:0:0
  #9 0x045ccfa5 GetFullTypeForDeclarator((anonymous 
namespace)::TypeProcessingState&, clang::QualType, clang::TypeSourceInfo*) 
SemaType.cpp:0:0
  ..

The reason of the failure is due to the mismatch of TypeLoc and 
D.getTypeObject().Kind. For example,
the TypeLoc is

  BTFTagAttributedType 0x88614e0 'int  btf_type_tag(rcu)()' sugar
  |-ParenType 0x8861480 'int ()' sugar
  | `-FunctionNoProtoType 0x8861450 'int ()' cdecl
  |   `-BuiltinType 0x87fd500 'int'

while corresponding D.getTypeObject().Kind points to DeclaratorChunk::Paren, 
and 
this will cause later assertion.

To fix the issue, similar to AttributedTypeLoc, let us skip 
BTFTagAttributedTypeLoc in
GetTypeSourceInfoForDeclarator().


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136807

Files:
  clang/lib/Sema/SemaType.cpp
  clang/test/Sema/attr-btf_type_tag-func-ptr.c


Index: clang/test/Sema/attr-btf_type_tag-func-ptr.c
===
--- /dev/null
+++ clang/test/Sema/attr-btf_type_tag-func-ptr.c
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -x c -triple x86_64-pc-linux-gnu -dwarf-version=4 
-fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+struct t {
+ int (__attribute__((btf_type_tag("rcu"))) *f)();
+ int a;
+};
+int foo(struct t *arg) {
+  return arg->a;
+}
Index: clang/lib/Sema/SemaType.cpp
===
--- clang/lib/Sema/SemaType.cpp
+++ clang/lib/Sema/SemaType.cpp
@@ -6516,6 +6516,9 @@
   CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
 }
 
+while (BTFTagAttributedTypeLoc TL = 
CurrTL.getAs())
+  CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
+
 while (DependentAddressSpaceTypeLoc TL =
CurrTL.getAs()) {
   fillDependentAddressSpaceTypeLoc(TL, D.getTypeObject(i).getAttrs());


Index: clang/test/Sema/attr-btf_type_tag-func-ptr.c
===
--- /dev/null
+++ clang/test/Sema/attr-btf_type_tag-func-ptr.c
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -x c -triple x86_64-pc-linux-gnu -dwarf-version=4 -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+struct t {
+ int (__attribute__((btf_type_tag("rcu"))) *f)();
+ int a;
+};
+int foo(struct t *arg) {
+  return arg->a;
+}
Index: clang/lib/Sema/SemaType.cpp
===
--- clang/lib/Sema/SemaType.cpp
+++ clang/lib/Sema/SemaType.cpp
@@ -6516,6 +6516,9 @@
   CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
 }
 
+while (BTFTagAttributedTypeLoc TL = CurrTL.getAs())
+  CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
+
 while (DependentAddressSpaceTypeLoc TL =
CurrTL.getAs()) {
   fillDependentAddressSpaceTypeLoc(TL, D.getTypeObject(i).getAttrs());
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D136041: [clang][DebugInfo] Emit DISubprogram for extern functions with reserved names

2022-10-18 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

In D136041#3863748 , @dblaikie wrote:

> Hmm - this does mean linking IR can produce invalid code, though, right (you 
> link in a definition of the function, so what was valid is now invalid - 
> because it now has a definition, can be inlined, etc)? Is that new? 
> concerning?

@dblaikie do you have a concrete example to illustrate the problem?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D136041/new/

https://reviews.llvm.org/D136041

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D136041: [clang][DebugInfo] Emit DISubprogram for extern functions with reserved names

2022-10-17 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

@aprantl The following example shows the difference between gcc and clang 
w.r.t. reserved names.

  [$ ~/tmp3] cat t.c
  extern void *__bpf_kptr_new(int, int, void *)
  __attribute__((section(".ksyms")));
  #define bpf_kptr_new(x) __bpf_kptr_new(x, 0, 0)
  
  struct foo {
  int data;
  };
  
  int main(void)
  {
  struct foo *f;
  
  f = bpf_kptr_new(0);
  return f->data;
  }
  [$ ~/tmp3] clang -target bpf -O2 -g -c t.c
  [$ ~/tmp3] llvm-dwarfdump t.o | grep bpf_kptr_new
  [$ ~/tmp3] gcc -O2 -g -c t.c
  [$ ~/tmp3] llvm-dwarfdump t.o | grep bpf_kptr_new
DW_AT_abstract_origin (0x00a3 "__bpf_kptr_new")
  DW_AT_linkage_name  ("__bpf_kptr_new")
  DW_AT_name  ("__bpf_kptr_new")
  [$ ~/tmp3]

If not using reserved name, the function will appear in debuginfo/dwarf:

  [$ ~/tmp3] cat t1.c
  extern void *bpf_kptr_new_(int, int, void *)
  __attribute__((section(".ksyms")));
  #define bpf_kptr_new(x) bpf_kptr_new_(x, 0, 0)
  
  struct foo {
  int data;
  };
  
  int main(void)
  {
  struct foo *f;
  
  f = bpf_kptr_new(0);
  return f->data;
  }
  [$ ~/tmp3] clang -target bpf -O2 -g -c t1.c
  [$ ~/tmp3] llvm-dwarfdump t1.o | grep bpf_kptr_new
  DW_AT_name  ("bpf_kptr_new_")
  [$~/tmp3] gcc -O2 -g -c t1.c
  [$ ~/tmp3] llvm-dwarfdump t1.o | grep bpf_kptr_new
DW_AT_abstract_origin (0x00a3 "bpf_kptr_new_")
  DW_AT_linkage_name  ("bpf_kptr_new_")
  DW_AT_name  ("bpf_kptr_new_")
  [$ ~/tmp3]

In BPF, we need the above extern function in debuginfo. This happens when a  
bpf program is calling a function defined in kernel. For example,
in kernel we have function

  int tcp_drop(struct sk_buff *skb, ...) { ...
  }
  /* allow tcp_drop() function accessible to bpf programs */

In bpf program, we could have

  extern int tcp_drop(struct sk_buff *sbk, ...);
  BPF_PROG(...) {
  tcp_drop(...)

BPF verifier will need to check whether the signature of tcp_drop() in bpf 
program matched the one in kernel or not. In kernel, we DO have some global 
functions with '__' prefix in the function name. Although no reserved function 
name works, we want all legal names working so user won't be surprised why some 
kernel functions are not available to them.

  }


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D136041/new/

https://reviews.llvm.org/D136041

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D134705: [clang][DebugInfo] Emit debuginfo for non-constant case value

2022-09-28 Thread Yonghong Song via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG75be0482a2e2: [clang][DebugInfo] Emit debuginfo for 
non-constant case value (authored by yonghong-song).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D134705/new/

https://reviews.llvm.org/D134705

Files:
  clang/lib/CodeGen/CGStmt.cpp
  clang/test/CodeGen/debug-info-enum-case-val.c


Index: clang/test/CodeGen/debug-info-enum-case-val.c
===
--- /dev/null
+++ clang/test/CodeGen/debug-info-enum-case-val.c
@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -emit-llvm -debug-info-kind=limited %s -o - | FileCheck %s
+
+enum { A = 1 };
+int func1(int a) {
+  switch(a) {
+  case A: return 10;
+  default: break;
+  }
+  return 0;
+}
+// CHECK:   !DICompositeType(tag: DW_TAG_enumeration_type
+// CHECK-SAME:  elements: [[TEST1_ENUMS:![0-9]*]]
+// CHECK:   [[TEST1_ENUMS]] = !{[[TEST1_E:![0-9]*]]}
+// CHECK:   [[TEST1_E]] = !DIEnumerator(name: "A", value: 1)
+
+// Test ImplicitCast of switch case enum value
+enum { B = 2 };
+typedef unsigned long long __t1;
+typedef __t1 __t2;
+int func2(__t2 a) {
+  switch(a) {
+  case B: return 10;
+  default: break;
+  }
+  return 0;
+}
+// CHECK:   !DICompositeType(tag: DW_TAG_enumeration_type
+// CHECK-SAME:  elements: [[TEST2_ENUMS:![0-9]*]]
+// CHECK:   [[TEST2_ENUMS]] = !{[[TEST2_E:![0-9]*]]}
+// CHECK:   [[TEST2_E]] = !DIEnumerator(name: "B", value: 2)
Index: clang/lib/CodeGen/CGStmt.cpp
===
--- clang/lib/CodeGen/CGStmt.cpp
+++ clang/lib/CodeGen/CGStmt.cpp
@@ -1509,6 +1509,21 @@
 
   llvm::ConstantInt *CaseVal =
 Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext()));
+
+  // Emit debuginfo for the case value if it is an enum value.
+  const ConstantExpr *CE;
+  if (auto ICE = dyn_cast(S.getLHS()))
+CE = dyn_cast(ICE->getSubExpr());
+  else
+CE = dyn_cast(S.getLHS());
+  if (CE) {
+if (auto DE = dyn_cast(CE->getSubExpr()))
+  if (CGDebugInfo *Dbg = getDebugInfo())
+if (CGM.getCodeGenOpts().hasReducedDebugInfo())
+  Dbg->EmitGlobalVariable(DE->getDecl(),
+  APValue(llvm::APSInt(CaseVal->getValue(;
+  }
+
   if (SwitchLikelihood)
 SwitchLikelihood->push_back(Stmt::getLikelihood(Attrs));
 


Index: clang/test/CodeGen/debug-info-enum-case-val.c
===
--- /dev/null
+++ clang/test/CodeGen/debug-info-enum-case-val.c
@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -emit-llvm -debug-info-kind=limited %s -o - | FileCheck %s
+
+enum { A = 1 };
+int func1(int a) {
+  switch(a) {
+  case A: return 10;
+  default: break;
+  }
+  return 0;
+}
+// CHECK:   !DICompositeType(tag: DW_TAG_enumeration_type
+// CHECK-SAME:  elements: [[TEST1_ENUMS:![0-9]*]]
+// CHECK:   [[TEST1_ENUMS]] = !{[[TEST1_E:![0-9]*]]}
+// CHECK:   [[TEST1_E]] = !DIEnumerator(name: "A", value: 1)
+
+// Test ImplicitCast of switch case enum value
+enum { B = 2 };
+typedef unsigned long long __t1;
+typedef __t1 __t2;
+int func2(__t2 a) {
+  switch(a) {
+  case B: return 10;
+  default: break;
+  }
+  return 0;
+}
+// CHECK:   !DICompositeType(tag: DW_TAG_enumeration_type
+// CHECK-SAME:  elements: [[TEST2_ENUMS:![0-9]*]]
+// CHECK:   [[TEST2_ENUMS]] = !{[[TEST2_E:![0-9]*]]}
+// CHECK:   [[TEST2_E]] = !DIEnumerator(name: "B", value: 2)
Index: clang/lib/CodeGen/CGStmt.cpp
===
--- clang/lib/CodeGen/CGStmt.cpp
+++ clang/lib/CodeGen/CGStmt.cpp
@@ -1509,6 +1509,21 @@
 
   llvm::ConstantInt *CaseVal =
 Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext()));
+
+  // Emit debuginfo for the case value if it is an enum value.
+  const ConstantExpr *CE;
+  if (auto ICE = dyn_cast(S.getLHS()))
+CE = dyn_cast(ICE->getSubExpr());
+  else
+CE = dyn_cast(S.getLHS());
+  if (CE) {
+if (auto DE = dyn_cast(CE->getSubExpr()))
+  if (CGDebugInfo *Dbg = getDebugInfo())
+if (CGM.getCodeGenOpts().hasReducedDebugInfo())
+  Dbg->EmitGlobalVariable(DE->getDecl(),
+  APValue(llvm::APSInt(CaseVal->getValue(;
+  }
+
   if (SwitchLikelihood)
 SwitchLikelihood->push_back(Stmt::getLikelihood(Attrs));
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D134705: [clang][DebugInfo] Emit debuginfo for non-constant case value

2022-09-28 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 463643.
yonghong-song added a comment.

- simplify test case, add more CHECK's and add comments to explain the test.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D134705/new/

https://reviews.llvm.org/D134705

Files:
  clang/lib/CodeGen/CGStmt.cpp
  clang/test/CodeGen/debug-info-enum-case-val.c


Index: clang/test/CodeGen/debug-info-enum-case-val.c
===
--- /dev/null
+++ clang/test/CodeGen/debug-info-enum-case-val.c
@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -emit-llvm -debug-info-kind=limited %s -o - | FileCheck %s
+
+enum { A = 1 };
+int func1(int a) {
+  switch(a) {
+  case A: return 10;
+  default: break;
+  }
+  return 0;
+}
+// CHECK:   !DICompositeType(tag: DW_TAG_enumeration_type
+// CHECK-SAME:  elements: [[TEST1_ENUMS:![0-9]*]]
+// CHECK:   [[TEST1_ENUMS]] = !{[[TEST1_E:![0-9]*]]}
+// CHECK:   [[TEST1_E]] = !DIEnumerator(name: "A", value: 1)
+
+// Test ImplicitCast of switch case enum value
+enum { B = 2 };
+typedef unsigned long long __t1;
+typedef __t1 __t2;
+int func2(__t2 a) {
+  switch(a) {
+  case B: return 10;
+  default: break;
+  }
+  return 0;
+}
+// CHECK:   !DICompositeType(tag: DW_TAG_enumeration_type
+// CHECK-SAME:  elements: [[TEST2_ENUMS:![0-9]*]]
+// CHECK:   [[TEST2_ENUMS]] = !{[[TEST2_E:![0-9]*]]}
+// CHECK:   [[TEST2_E]] = !DIEnumerator(name: "B", value: 2)
Index: clang/lib/CodeGen/CGStmt.cpp
===
--- clang/lib/CodeGen/CGStmt.cpp
+++ clang/lib/CodeGen/CGStmt.cpp
@@ -1509,6 +1509,21 @@
 
   llvm::ConstantInt *CaseVal =
 Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext()));
+
+  // Emit debuginfo for the case value if it is an enum value.
+  const ConstantExpr *CE;
+  if (auto ICE = dyn_cast(S.getLHS()))
+CE = dyn_cast(ICE->getSubExpr());
+  else
+CE = dyn_cast(S.getLHS());
+  if (CE) {
+if (auto DE = dyn_cast(CE->getSubExpr()))
+  if (CGDebugInfo *Dbg = getDebugInfo())
+if (CGM.getCodeGenOpts().hasReducedDebugInfo())
+  Dbg->EmitGlobalVariable(DE->getDecl(),
+  APValue(llvm::APSInt(CaseVal->getValue(;
+  }
+
   if (SwitchLikelihood)
 SwitchLikelihood->push_back(Stmt::getLikelihood(Attrs));
 


Index: clang/test/CodeGen/debug-info-enum-case-val.c
===
--- /dev/null
+++ clang/test/CodeGen/debug-info-enum-case-val.c
@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -emit-llvm -debug-info-kind=limited %s -o - | FileCheck %s
+
+enum { A = 1 };
+int func1(int a) {
+  switch(a) {
+  case A: return 10;
+  default: break;
+  }
+  return 0;
+}
+// CHECK:   !DICompositeType(tag: DW_TAG_enumeration_type
+// CHECK-SAME:  elements: [[TEST1_ENUMS:![0-9]*]]
+// CHECK:   [[TEST1_ENUMS]] = !{[[TEST1_E:![0-9]*]]}
+// CHECK:   [[TEST1_E]] = !DIEnumerator(name: "A", value: 1)
+
+// Test ImplicitCast of switch case enum value
+enum { B = 2 };
+typedef unsigned long long __t1;
+typedef __t1 __t2;
+int func2(__t2 a) {
+  switch(a) {
+  case B: return 10;
+  default: break;
+  }
+  return 0;
+}
+// CHECK:   !DICompositeType(tag: DW_TAG_enumeration_type
+// CHECK-SAME:  elements: [[TEST2_ENUMS:![0-9]*]]
+// CHECK:   [[TEST2_ENUMS]] = !{[[TEST2_E:![0-9]*]]}
+// CHECK:   [[TEST2_E]] = !DIEnumerator(name: "B", value: 2)
Index: clang/lib/CodeGen/CGStmt.cpp
===
--- clang/lib/CodeGen/CGStmt.cpp
+++ clang/lib/CodeGen/CGStmt.cpp
@@ -1509,6 +1509,21 @@
 
   llvm::ConstantInt *CaseVal =
 Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext()));
+
+  // Emit debuginfo for the case value if it is an enum value.
+  const ConstantExpr *CE;
+  if (auto ICE = dyn_cast(S.getLHS()))
+CE = dyn_cast(ICE->getSubExpr());
+  else
+CE = dyn_cast(S.getLHS());
+  if (CE) {
+if (auto DE = dyn_cast(CE->getSubExpr()))
+  if (CGDebugInfo *Dbg = getDebugInfo())
+if (CGM.getCodeGenOpts().hasReducedDebugInfo())
+  Dbg->EmitGlobalVariable(DE->getDecl(),
+  APValue(llvm::APSInt(CaseVal->getValue(;
+  }
+
   if (SwitchLikelihood)
 SwitchLikelihood->push_back(Stmt::getLikelihood(Attrs));
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D134705: [clang][DebugInfo] Emit debuginfo for non-constant case value

2022-09-28 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

In D134705#3818657 , @dblaikie wrote:

> SGTM - could you also add some CHECKs in the test that show the enumeration 
> was added to the enumerators list on the CU (& you can probably test with 
> just one enumerator - don't need two in each test for instance)

Okay, will do.

> & I'm not quite following - what's the difference between the two cases being 
> tested? Oh, I guess the second has implicit conversions in the case 
> parameters? Fair enough, maybe would benefit from some comments.

Yes, the first is without implicit conversion and the second is with implicit 
conversion. I will add some comments to clarify it in the test.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D134705/new/

https://reviews.llvm.org/D134705

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D134705: [clang][DebugInfo] Emit debuginfo for non-constant case value

2022-09-27 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song created this revision.
yonghong-song added a reviewer: dblaikie.
Herald added a project: All.
yonghong-song requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Currently, clang does not emit debuginfo for the switch stmt
case value if it is an enum value. For example,

  $ cat test.c
  enum { AA = 1, BB = 2 };
  int func1(int a) {
switch(a) {
case AA: return 10; 
case BB: return 11; 
default: break;
}   
return 0;
  }
  $ llvm-dwarfdump test.o | grep AA
  $

Note that gcc does emit debuginfo for the same test case.

This patch added such a support with similar implementation
to CodeGenFunction::EmitDeclRefExprDbgValue(). With this patch,

  $ clang -g -c test.c
  $ llvm-dwarfdump test.o | grep AA
  DW_AT_name("AA")
  $   


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D134705

Files:
  clang/lib/CodeGen/CGStmt.cpp
  clang/test/CodeGen/debug-info-enum-case-val.c


Index: clang/test/CodeGen/debug-info-enum-case-val.c
===
--- /dev/null
+++ clang/test/CodeGen/debug-info-enum-case-val.c
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -emit-llvm -debug-info-kind=limited %s -o - | FileCheck %s
+
+enum { A = 1, B = 2 };
+int func1(int a) {
+  switch(a) {
+  case A: return 10;
+  case B: return 11;
+  default: break;
+  }
+  return 0;
+}
+// CHECK:   !DIEnumerator(name: "A", value: 1)
+// CHECK-NEXT:  !DIEnumerator(name: "B", value: 2)
+
+enum { C = 1, D = 2 };
+typedef unsigned long long __t1;
+typedef __t1 __t2;
+int func2(__t2 a) {
+  switch(a) {
+  case C: return 10;
+  case D: return 11;
+  default: break;
+  }
+  return 0;
+}
+// CHECK:   !DIEnumerator(name: "C", value: 1)
+// CHECK-NEXT:  !DIEnumerator(name: "D", value: 2)
Index: clang/lib/CodeGen/CGStmt.cpp
===
--- clang/lib/CodeGen/CGStmt.cpp
+++ clang/lib/CodeGen/CGStmt.cpp
@@ -1509,6 +1509,21 @@
 
   llvm::ConstantInt *CaseVal =
 Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext()));
+
+  // Emit debuginfo for the case value if it is an enum value.
+  const ConstantExpr *CE;
+  if (auto ICE = dyn_cast(S.getLHS()))
+CE = dyn_cast(ICE->getSubExpr());
+  else
+CE = dyn_cast(S.getLHS());
+  if (CE) {
+if (auto DE = dyn_cast(CE->getSubExpr()))
+  if (CGDebugInfo *Dbg = getDebugInfo())
+if (CGM.getCodeGenOpts().hasReducedDebugInfo())
+  Dbg->EmitGlobalVariable(DE->getDecl(),
+  APValue(llvm::APSInt(CaseVal->getValue(;
+  }
+
   if (SwitchLikelihood)
 SwitchLikelihood->push_back(Stmt::getLikelihood(Attrs));
 


Index: clang/test/CodeGen/debug-info-enum-case-val.c
===
--- /dev/null
+++ clang/test/CodeGen/debug-info-enum-case-val.c
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -emit-llvm -debug-info-kind=limited %s -o - | FileCheck %s
+
+enum { A = 1, B = 2 };
+int func1(int a) {
+  switch(a) {
+  case A: return 10;
+  case B: return 11;
+  default: break;
+  }
+  return 0;
+}
+// CHECK:   !DIEnumerator(name: "A", value: 1)
+// CHECK-NEXT:  !DIEnumerator(name: "B", value: 2)
+
+enum { C = 1, D = 2 };
+typedef unsigned long long __t1;
+typedef __t1 __t2;
+int func2(__t2 a) {
+  switch(a) {
+  case C: return 10;
+  case D: return 11;
+  default: break;
+  }
+  return 0;
+}
+// CHECK:   !DIEnumerator(name: "C", value: 1)
+// CHECK-NEXT:  !DIEnumerator(name: "D", value: 2)
Index: clang/lib/CodeGen/CGStmt.cpp
===
--- clang/lib/CodeGen/CGStmt.cpp
+++ clang/lib/CodeGen/CGStmt.cpp
@@ -1509,6 +1509,21 @@
 
   llvm::ConstantInt *CaseVal =
 Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext()));
+
+  // Emit debuginfo for the case value if it is an enum value.
+  const ConstantExpr *CE;
+  if (auto ICE = dyn_cast(S.getLHS()))
+CE = dyn_cast(ICE->getSubExpr());
+  else
+CE = dyn_cast(S.getLHS());
+  if (CE) {
+if (auto DE = dyn_cast(CE->getSubExpr()))
+  if (CGDebugInfo *Dbg = getDebugInfo())
+if (CGM.getCodeGenOpts().hasReducedDebugInfo())
+  Dbg->EmitGlobalVariable(DE->getDecl(),
+  APValue(llvm::APSInt(CaseVal->getValue(;
+  }
+
   if (SwitchLikelihood)
 SwitchLikelihood->push_back(Stmt::getLikelihood(Attrs));
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D132144: [Clang][BPF] Support record argument with direct values

2022-08-18 Thread Yonghong Song via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG481d67d310a7: [Clang][BPF] Support record argument with 
direct values (authored by yonghong-song).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D132144/new/

https://reviews.llvm.org/D132144

Files:
  clang/lib/CodeGen/TargetInfo.cpp
  clang/test/CodeGen/bpf-struct-argument.c
  clang/test/CodeGen/bpf-union-argument.c
  llvm/test/CodeGen/BPF/struct-arg-inline.ll
  llvm/test/CodeGen/BPF/struct-arg.ll

Index: llvm/test/CodeGen/BPF/struct-arg.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/struct-arg.ll
@@ -0,0 +1,56 @@
+; RUN: llc < %s -march=bpfel | FileCheck %s
+; Source:
+;   struct t1 {
+; long a;
+;   };
+;   struct t2 {
+; long a;
+; long b;
+;   };
+;   long foo2(struct t1 a1, struct t1 a2, struct t1 a3, struct t1 a4, struct t1 a5) {
+; return a1.a + a2.a + a3.a + a4.a + a5.a;
+;   }
+;   long foo3(struct t2 a1, struct t2 a2, struct t1 a3) {
+; return a1.a + a2.a + a3.a;
+;   }
+; Compilation flags:
+;   clang -target bpf -S -emit-llvm -O2 t.c
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind readnone willreturn
+define dso_local i64 @foo2(i64 %a1.coerce, i64 %a2.coerce, i64 %a3.coerce, i64 %a4.coerce, i64 %a5.coerce) local_unnamed_addr #0 {
+entry:
+  %add = add nsw i64 %a2.coerce, %a1.coerce
+  %add8 = add nsw i64 %add, %a3.coerce
+  %add10 = add nsw i64 %add8, %a4.coerce
+  %add12 = add nsw i64 %add10, %a5.coerce
+  ret i64 %add12
+; CHECK:r0 = r2
+; CHECK:r0 += r1
+; CHECK:r0 += r3
+; CHECK:r0 += r4
+; CHECK:r0 += r5
+; CHECK-NEXT:   exit
+}
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind readnone willreturn
+define dso_local i64 @foo3([2 x i64] %a1.coerce, [2 x i64] %a2.coerce, i64 %a3.coerce) local_unnamed_addr #0 {
+entry:
+  %a1.coerce.fca.0.extract = extractvalue [2 x i64] %a1.coerce, 0
+  %a2.coerce.fca.0.extract = extractvalue [2 x i64] %a2.coerce, 0
+  %add = add nsw i64 %a2.coerce.fca.0.extract, %a1.coerce.fca.0.extract
+  %add6 = add nsw i64 %add, %a3.coerce
+  ret i64 %add6
+; CHECK:r0 = r3
+; CHECK-NEXT:   r0 += r1
+; CHECK-NEXT:   r0 += r5
+; CHECK-NEXT:   exit
+}
+
+attributes #0 = { mustprogress nofree norecurse nosync nounwind readnone willreturn "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+
+!llvm.module.flags = !{!0, !1}
+!llvm.ident = !{!2}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 7, !"frame-pointer", i32 2}
+!2 = !{!"clang version 16.0.0 (https://github.com/llvm/llvm-project.git 9385660f4ca87d074410a84df89faca313afcb5a)"}
Index: llvm/test/CodeGen/BPF/struct-arg-inline.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/struct-arg-inline.ll
@@ -0,0 +1,108 @@
+; RUN: opt -O2 -mtriple=bpf-pc-linux %s | llvm-dis > %t1
+; RUN: llc %t1 -o - | FileCheck %s
+; Source:
+;   struct t1 {
+; long a;
+;   };
+;   struct t2 {
+; long a;
+; long b;
+;   };
+;   __attribute__((always_inline))
+;   static long foo1(struct t2 a1, struct t1 a2, struct t1 a3, struct t1 a4,
+;struct t1 a5, struct t2 a6) {
+; return a1.a + a2.a + a3.a + a4.a + a5.a + a6.a;
+;   }
+;   long foo2(struct t2 a1, struct t2 a2, struct t1 a3) {
+; return foo1(a1, a3, a3, a3, a3, a2);
+;   }
+; Compilation flags:
+;   clang -target bpf -O2 -S -emit-llvm -Xclang -disable-llvm-passes t.c
+
+%struct.t2 = type { i64, i64 }
+%struct.t1 = type { i64 }
+
+; Function Attrs: nounwind
+define dso_local i64 @foo2([2 x i64] %a1.coerce, [2 x i64] %a2.coerce, i64 %a3.coerce) #0 {
+entry:
+  %a1 = alloca %struct.t2, align 8
+  %a2 = alloca %struct.t2, align 8
+  %a3 = alloca %struct.t1, align 8
+  store [2 x i64] %a1.coerce, ptr %a1, align 8
+  store [2 x i64] %a2.coerce, ptr %a2, align 8
+  %coerce.dive = getelementptr inbounds %struct.t1, ptr %a3, i32 0, i32 0
+  store i64 %a3.coerce, ptr %coerce.dive, align 8
+  %0 = load [2 x i64], ptr %a1, align 8
+  %coerce.dive1 = getelementptr inbounds %struct.t1, ptr %a3, i32 0, i32 0
+  %1 = load i64, ptr %coerce.dive1, align 8
+  %coerce.dive2 = getelementptr inbounds %struct.t1, ptr %a3, i32 0, i32 0
+  %2 = load i64, ptr %coerce.dive2, align 8
+  %coerce.dive3 = getelementptr inbounds %struct.t1, ptr %a3, i32 0, i32 0
+  %3 = load i64, ptr %coerce.dive3, align 8
+  %coerce.dive4 = getelementptr inbounds %struct.t1, ptr %a3, i32 0, i32 0
+  %4 = load i64, ptr %coerce.dive4, align 8
+  %5 = load [2 x i64], ptr %a2, align 8
+  %call = call i64 @foo1([2 x i64] %0, i64 %1, i64 %2, i64 %3, i64 %4, [2 x i64] %5)
+  ret i64 %call
+; CHECK: r0 = r3
+; CHECK-NEXT:r0 += r1
+; CHECK-NEXT:r5 <<= 2
+; CHECK-NEXT:r0 += r5
+; CHECK-NEXT:

[PATCH] D132144: [Clang][BPF] Support record argument with direct values

2022-08-18 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 453835.
yonghong-song added a comment.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

add two llvm/test/CodeGen/BPF tests.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D132144/new/

https://reviews.llvm.org/D132144

Files:
  clang/lib/CodeGen/TargetInfo.cpp
  clang/test/CodeGen/bpf-struct-argument.c
  clang/test/CodeGen/bpf-union-argument.c
  llvm/test/CodeGen/BPF/struct-arg-inline.ll
  llvm/test/CodeGen/BPF/struct-arg.ll

Index: llvm/test/CodeGen/BPF/struct-arg.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/struct-arg.ll
@@ -0,0 +1,56 @@
+; RUN: llc < %s -march=bpfel | FileCheck %s
+; Source:
+;   struct t1 {
+; long a;
+;   };
+;   struct t2 {
+; long a;
+; long b;
+;   };
+;   long foo2(struct t1 a1, struct t1 a2, struct t1 a3, struct t1 a4, struct t1 a5) {
+; return a1.a + a2.a + a3.a + a4.a + a5.a;
+;   }
+;   long foo3(struct t2 a1, struct t2 a2, struct t1 a3) {
+; return a1.a + a2.a + a3.a;
+;   }
+; Compilation flags:
+;   clang -target bpf -S -emit-llvm -O2 t.c
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind readnone willreturn
+define dso_local i64 @foo2(i64 %a1.coerce, i64 %a2.coerce, i64 %a3.coerce, i64 %a4.coerce, i64 %a5.coerce) local_unnamed_addr #0 {
+entry:
+  %add = add nsw i64 %a2.coerce, %a1.coerce
+  %add8 = add nsw i64 %add, %a3.coerce
+  %add10 = add nsw i64 %add8, %a4.coerce
+  %add12 = add nsw i64 %add10, %a5.coerce
+  ret i64 %add12
+; CHECK:r0 = r2
+; CHECK:r0 += r1
+; CHECK:r0 += r3
+; CHECK:r0 += r4
+; CHECK:r0 += r5
+; CHECK-NEXT:   exit
+}
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind readnone willreturn
+define dso_local i64 @foo3([2 x i64] %a1.coerce, [2 x i64] %a2.coerce, i64 %a3.coerce) local_unnamed_addr #0 {
+entry:
+  %a1.coerce.fca.0.extract = extractvalue [2 x i64] %a1.coerce, 0
+  %a2.coerce.fca.0.extract = extractvalue [2 x i64] %a2.coerce, 0
+  %add = add nsw i64 %a2.coerce.fca.0.extract, %a1.coerce.fca.0.extract
+  %add6 = add nsw i64 %add, %a3.coerce
+  ret i64 %add6
+; CHECK:r0 = r3
+; CHECK-NEXT:   r0 += r1
+; CHECK-NEXT:   r0 += r5
+; CHECK-NEXT:   exit
+}
+
+attributes #0 = { mustprogress nofree norecurse nosync nounwind readnone willreturn "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+
+!llvm.module.flags = !{!0, !1}
+!llvm.ident = !{!2}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 7, !"frame-pointer", i32 2}
+!2 = !{!"clang version 16.0.0 (https://github.com/llvm/llvm-project.git 9385660f4ca87d074410a84df89faca313afcb5a)"}
Index: llvm/test/CodeGen/BPF/struct-arg-inline.ll
===
--- /dev/null
+++ llvm/test/CodeGen/BPF/struct-arg-inline.ll
@@ -0,0 +1,108 @@
+; RUN: opt -O2 -mtriple=bpf-pc-linux %s | llvm-dis > %t1
+; RUN: llc %t1 -o - | FileCheck %s
+; Source:
+;   struct t1 {
+; long a;
+;   };
+;   struct t2 {
+; long a;
+; long b;
+;   };
+;   __attribute__((always_inline))
+;   static long foo1(struct t2 a1, struct t1 a2, struct t1 a3, struct t1 a4,
+;struct t1 a5, struct t2 a6) {
+; return a1.a + a2.a + a3.a + a4.a + a5.a + a6.a;
+;   }
+;   long foo2(struct t2 a1, struct t2 a2, struct t1 a3) {
+; return foo1(a1, a3, a3, a3, a3, a2);
+;   }
+; Compilation flags:
+;   clang -target bpf -O2 -S -emit-llvm -Xclang -disable-llvm-passes t.c
+
+%struct.t2 = type { i64, i64 }
+%struct.t1 = type { i64 }
+
+; Function Attrs: nounwind
+define dso_local i64 @foo2([2 x i64] %a1.coerce, [2 x i64] %a2.coerce, i64 %a3.coerce) #0 {
+entry:
+  %a1 = alloca %struct.t2, align 8
+  %a2 = alloca %struct.t2, align 8
+  %a3 = alloca %struct.t1, align 8
+  store [2 x i64] %a1.coerce, ptr %a1, align 8
+  store [2 x i64] %a2.coerce, ptr %a2, align 8
+  %coerce.dive = getelementptr inbounds %struct.t1, ptr %a3, i32 0, i32 0
+  store i64 %a3.coerce, ptr %coerce.dive, align 8
+  %0 = load [2 x i64], ptr %a1, align 8
+  %coerce.dive1 = getelementptr inbounds %struct.t1, ptr %a3, i32 0, i32 0
+  %1 = load i64, ptr %coerce.dive1, align 8
+  %coerce.dive2 = getelementptr inbounds %struct.t1, ptr %a3, i32 0, i32 0
+  %2 = load i64, ptr %coerce.dive2, align 8
+  %coerce.dive3 = getelementptr inbounds %struct.t1, ptr %a3, i32 0, i32 0
+  %3 = load i64, ptr %coerce.dive3, align 8
+  %coerce.dive4 = getelementptr inbounds %struct.t1, ptr %a3, i32 0, i32 0
+  %4 = load i64, ptr %coerce.dive4, align 8
+  %5 = load [2 x i64], ptr %a2, align 8
+  %call = call i64 @foo1([2 x i64] %0, i64 %1, i64 %2, i64 %3, i64 %4, [2 x i64] %5)
+  ret i64 %call
+; CHECK: r0 = r3
+; CHECK-NEXT:r0 += r1
+; CHECK-NEXT:r5 <<= 2
+; CHECK-NEXT:r0 += r5
+; CHECK-NEXT:exit
+}
+
+; Function Attrs: alwaysinline nounwind

[PATCH] D132144: [Clang][BPF] Support record argument with direct values

2022-08-18 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

>   but if the function is always_inline it can have more than 5 arguments, 
> right?

That is correct. Will add a test case for this.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D132144/new/

https://reviews.llvm.org/D132144

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D132144: [Clang][BPF] Support record argument with direct values

2022-08-18 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

> Kernel changes were assuming at most 2

My new version code allows all 5 parameters to be a structure argument.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D132144/new/

https://reviews.llvm.org/D132144

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D132144: [Clang][BPF] Support record argument with direct values

2022-08-18 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

The compiler only counted the total number of registers won't exceed 5. You 
could have 5 struct arguments (each one register), or 2 struct arguments (with 
two registers each) and one integer register.
Whether to pass by value or by reference is a ABI issue, not related to the 
number of arguments. Checking whether arguments are enough or not is left to 
BPF backend lowering.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D132144/new/

https://reviews.llvm.org/D132144

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D132144: [Clang][BPF] Support record argument with direct values

2022-08-18 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

In D132144#3732685 , @anakryiko wrote:

> Are there any restrictions about number of struct arguments that can be 
> passed in? Kernel changes were assuming at most 2, should we have a test that 
> tests passing 3 structs that fit into 5 input registers and another test that 
> passes 3 structs that do not fit in 5 input registers? And what should be the 
> behavior in the latter case -- compilation error or switch to "pass by 
> reference"?

This is a good question. I actually tried to add such a test but unfortunately 
didn't find a good way to do it.
The following is an example,

  [$ ~/tmp3] cat t2.c
  struct t1 {
long a;
  };
  struct t2 {
long a;
long b;
  };
  
  long foo1(long a1, long a2, long a3, long a4, long a5) {
return a1 + a2 + a3 + a4 + a5;
  }
  
  long foo2(struct t1 a1, long a2, long a3, long a4, long a5) {
return a1.a + a2 + a3 + a4 + a5;
  }
  
  long foo3(struct t2 a1, long a2, long a3, long a4, long a5) {
return a1.a + a2 + a3 + a4 + a5;
  }
  [$ ~/tmp3] clang -target bpf -O2 -emit-llvm -S -Xclang -disable-llvm-passes 
t2.c
  [$ ~/tmp3] clang -target bpf -O2 -emit-llvm -S t2.c
  [$ ~/tmp3] clang -target bpf -O2 -S t2.c
  t2.c:17:6: error: defined with too many args
  long foo3(struct t2 a1, long a2, long a3, long a4, long a5) {
   ^
  1 error generated.
  [$~/tmp3]

You can see compiler does not flag the error during IR generation and IR 
optimization.
The error is actually flagged out during IR->Machine IR translation in file

  Target/BPF/BPFISelLowering.cpp:  fail(DL, DAG, "defined with too many 
args");

Because the error is trigged by the BPF backend, which means that .s or .o file 
will
not get generated so we could not really test it. This is the case for all other
backend triggered fail or fatal_error.

But I think I will add a llvm test from IR to asm code which will include 
struct arguments (16 bytes)
and it will extend to use 5 registers for all arguments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D132144/new/

https://reviews.llvm.org/D132144

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D132144: [Clang][BPF] Support record argument with direct values

2022-08-18 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song created this revision.
yonghong-song added a reviewer: ast.
Herald added subscribers: pengfei, kristof.beyls.
Herald added a project: All.
yonghong-song requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Currently, record arguments are always passed by reference by allocating
space for record values in the caller. This is less efficient for 
small records which may take one or two registers. For example,
for x86_64 and aarch64, for a record size up to 16 bytes, the record
values can be passed by values directly on the registers.

This patch added BPF support of record argument with direct values
for up to 16 byte record size. If record size is 0, that record
will not take any register, which is the same behavior for x86_64
and aarch64. If the record size is greater than 16 bytes, the 
record argument will be passed by reference.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D132144

Files:
  clang/lib/CodeGen/TargetInfo.cpp
  clang/test/CodeGen/bpf-struct-argument.c
  clang/test/CodeGen/bpf-union-argument.c

Index: clang/test/CodeGen/bpf-union-argument.c
===
--- /dev/null
+++ clang/test/CodeGen/bpf-union-argument.c
@@ -0,0 +1,44 @@
+// REQUIRES: bpf-registered-target
+// RUN: %clang_cc1 -triple bpf -O2 -emit-llvm -disable-llvm-passes %s -o - | FileCheck %s
+
+union t1 {};
+union t2 {
+  int a;
+  long b;
+};
+union t3 {
+  struct {
+int a;
+long b;
+  };
+  long c;
+};
+union t4 {
+  struct {
+long a;
+long b;
+long c;
+  };
+  long d;
+};
+
+int foo1(union t1 arg1, union t2 arg2) {
+// CHECK: define dso_local i32 @foo1(i64 %arg2.coerce)
+  return arg2.a;
+}
+
+int foo2(union t3 arg1, union t4 arg2) {
+// CHECK: define dso_local i32 @foo2([2 x i64] %arg1.coerce, ptr noundef byval(%union.t4) align 8 %arg2)
+  return arg1.a + arg2.a;
+
+}
+
+int foo3(void) {
+  union t1 tmp1 = {};
+  union t2 tmp2 = {};
+  union t3 tmp3 = {};
+  union t4 tmp4 = {};
+  return foo1(tmp1, tmp2) + foo2(tmp3, tmp4);
+// CHECK: call i32 @foo1(i64 %{{[a-zA-Z0-9]+}})
+// CHECK: call i32 @foo2([2 x i64] %{{[a-zA-Z0-9]+}}, ptr noundef byval(%union.t4) align 8 %tmp4)
+}
Index: clang/test/CodeGen/bpf-struct-argument.c
===
--- /dev/null
+++ clang/test/CodeGen/bpf-struct-argument.c
@@ -0,0 +1,36 @@
+// REQUIRES: bpf-registered-target
+// RUN: %clang_cc1 -triple bpf -O2 -emit-llvm -disable-llvm-passes %s -o - | FileCheck %s
+
+struct t1 {};
+struct t2 {
+  int a;
+};
+struct t3 {
+  int a;
+  long b;
+};
+struct t4 {
+  long a;
+  long b;
+  long c;
+};
+
+int foo1(struct t1 arg1, struct t2 arg2) {
+// CHECK: define dso_local i32 @foo1(i32 %arg2.coerce)
+  return arg2.a;
+}
+
+int foo2(struct t3 arg1, struct t4 arg2) {
+// CHECK: define dso_local i32 @foo2([2 x i64] %arg1.coerce, ptr noundef byval(%struct.t4) align 8 %arg2)
+  return arg1.a + arg2.a;
+}
+
+int foo3(void) {
+  struct t1 tmp1 = {};
+  struct t2 tmp2 = {};
+  struct t3 tmp3 = {};
+  struct t4 tmp4 = {};
+  return foo1(tmp1, tmp2) + foo2(tmp3, tmp4);
+// CHECK: call i32 @foo1(i32 %{{[a-zA-Z0-9]+}})
+// CHECK: call i32 @foo2([2 x i64] %{{[a-zA-Z0-9]+}}, ptr noundef byval(%struct.t4) align 8 %tmp4)
+}
Index: clang/lib/CodeGen/TargetInfo.cpp
===
--- clang/lib/CodeGen/TargetInfo.cpp
+++ clang/lib/CodeGen/TargetInfo.cpp
@@ -11509,6 +11509,42 @@
 public:
   BPFABIInfo(CodeGenTypes ) : DefaultABIInfo(CGT) {}
 
+  ABIArgInfo classifyArgumentType(QualType Ty) const {
+Ty = useFirstFieldIfTransparentUnion(Ty);
+
+if (isAggregateTypeForABI(Ty)) {
+  uint64_t Bits = getContext().getTypeSize(Ty);
+  if (Bits == 0)
+return ABIArgInfo::getIgnore();
+
+  // If the aggregate needs 1 or 2 registers, do not use reference.
+  if (Bits <= 128) {
+llvm::Type *CoerceTy;
+if (Bits <= 64) {
+  CoerceTy =
+  llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
+} else {
+  llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), 64);
+  CoerceTy = llvm::ArrayType::get(RegTy, 2);
+}
+return ABIArgInfo::getDirect(CoerceTy);
+  } else {
+return getNaturalAlignIndirect(Ty);
+  }
+}
+
+if (const EnumType *EnumTy = Ty->getAs())
+  Ty = EnumTy->getDecl()->getIntegerType();
+
+ASTContext  = getContext();
+if (const auto *EIT = Ty->getAs())
+  if (EIT->getNumBits() > Context.getTypeSize(Context.Int128Ty))
+return getNaturalAlignIndirect(Ty);
+
+return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
+  : ABIArgInfo::getDirect());
+  }
+
   ABIArgInfo classifyReturnType(QualType RetTy) const {
 if (RetTy->isVoidType())
   return ABIArgInfo::getIgnore();

[PATCH] D131598: [Clang][BPF]: Force sign/zero extension for return values in caller

2022-08-16 Thread Yonghong Song via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGd9198f64d9be: [Clang][BPF]: Force sign/zero extension for 
return values in caller (authored by yonghong-song).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D131598/new/

https://reviews.llvm.org/D131598

Files:
  clang/lib/CodeGen/TargetInfo.cpp
  clang/test/CodeGen/bpf-abiinfo.c


Index: clang/test/CodeGen/bpf-abiinfo.c
===
--- /dev/null
+++ clang/test/CodeGen/bpf-abiinfo.c
@@ -0,0 +1,24 @@
+// REQUIRES: bpf-registered-target
+// RUN: %clang_cc1 -triple bpf -O2 -emit-llvm -disable-llvm-passes %s -o - | 
FileCheck %s
+
+_Bool bar_bool(void);
+unsigned char bar_char(void);
+short bar_short(void);
+int bar_int(void);
+
+int foo_bool(void) {
+if (bar_bool() != 1) return 0; else return 1;
+}
+// CHECK: %call = call i1 @bar_bool()
+int foo_char(void) {
+if (bar_char() != 10) return 0; else return 1;
+}
+// CHECK: %call = call i8 @bar_char()
+int foo_short(void) {
+if (bar_short() != 10) return 0; else return 1;
+}
+// CHECK: %call = call i16 @bar_short()
+int foo_int(void) {
+if (bar_int() != 10) return 0; else return 1;
+}
+// CHECK: %call = call i32 @bar_int()
Index: clang/lib/CodeGen/TargetInfo.cpp
===
--- clang/lib/CodeGen/TargetInfo.cpp
+++ clang/lib/CodeGen/TargetInfo.cpp
@@ -11499,6 +11499,56 @@
 };
 } // end anonymous namespace
 
+//===--===//
+// BPF ABI Implementation
+//===--===//
+
+namespace {
+
+class BPFABIInfo : public DefaultABIInfo {
+public:
+  BPFABIInfo(CodeGenTypes ) : DefaultABIInfo(CGT) {}
+
+  ABIArgInfo classifyReturnType(QualType RetTy) const {
+if (RetTy->isVoidType())
+  return ABIArgInfo::getIgnore();
+
+if (isAggregateTypeForABI(RetTy))
+  return getNaturalAlignIndirect(RetTy);
+
+// Treat an enum type as its underlying type.
+if (const EnumType *EnumTy = RetTy->getAs())
+  RetTy = EnumTy->getDecl()->getIntegerType();
+
+ASTContext  = getContext();
+if (const auto *EIT = RetTy->getAs())
+  if (EIT->getNumBits() > Context.getTypeSize(Context.Int128Ty))
+return getNaturalAlignIndirect(RetTy);
+
+// Caller will do necessary sign/zero extension.
+return ABIArgInfo::getDirect();
+  }
+
+  void computeInfo(CGFunctionInfo ) const override {
+FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
+for (auto  : FI.arguments())
+  I.info = classifyArgumentType(I.type);
+  }
+
+};
+
+class BPFTargetCodeGenInfo : public TargetCodeGenInfo {
+public:
+  BPFTargetCodeGenInfo(CodeGenTypes )
+  : TargetCodeGenInfo(std::make_unique(CGT)) {}
+
+  const BPFABIInfo () const {
+return static_cast(TargetCodeGenInfo::getABIInfo());
+  }
+};
+
+}
+
 
//===--===//
 // Driver code
 
//===--===//
@@ -11727,6 +11777,9 @@
   : hasFP64   ? 64
   : 32));
   }
+  case llvm::Triple::bpfeb:
+  case llvm::Triple::bpfel:
+return SetCGInfo(new BPFTargetCodeGenInfo(Types));
   }
 }
 


Index: clang/test/CodeGen/bpf-abiinfo.c
===
--- /dev/null
+++ clang/test/CodeGen/bpf-abiinfo.c
@@ -0,0 +1,24 @@
+// REQUIRES: bpf-registered-target
+// RUN: %clang_cc1 -triple bpf -O2 -emit-llvm -disable-llvm-passes %s -o - | FileCheck %s
+
+_Bool bar_bool(void);
+unsigned char bar_char(void);
+short bar_short(void);
+int bar_int(void);
+
+int foo_bool(void) {
+if (bar_bool() != 1) return 0; else return 1;
+}
+// CHECK: %call = call i1 @bar_bool()
+int foo_char(void) {
+if (bar_char() != 10) return 0; else return 1;
+}
+// CHECK: %call = call i8 @bar_char()
+int foo_short(void) {
+if (bar_short() != 10) return 0; else return 1;
+}
+// CHECK: %call = call i16 @bar_short()
+int foo_int(void) {
+if (bar_int() != 10) return 0; else return 1;
+}
+// CHECK: %call = call i32 @bar_int()
Index: clang/lib/CodeGen/TargetInfo.cpp
===
--- clang/lib/CodeGen/TargetInfo.cpp
+++ clang/lib/CodeGen/TargetInfo.cpp
@@ -11499,6 +11499,56 @@
 };
 } // end anonymous namespace
 
+//===--===//
+// BPF ABI Implementation
+//===--===//
+
+namespace {
+
+class BPFABIInfo : public DefaultABIInfo {
+public:
+  BPFABIInfo(CodeGenTypes ) : DefaultABIInfo(CGT) {}

[PATCH] D131598: [Clang][BPF]: Force sign/zero extension for return values in caller

2022-08-10 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song created this revision.
yonghong-song added reviewers: ast, anakryiko.
Herald added subscribers: pengfei, kristof.beyls.
Herald added a project: All.
yonghong-song requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Currently bpf supports calling kernel functions (x86_64, arm64, etc.)
in bpf programs. Tejun discovered a problem where the x86_64 func
return value (a unsigned char type) is stored in 8-bit subregister %al
and the other 56-bits in %rax might be garbage. But based on current
bpf ABI, the bpf program assumes the whole %rax holds the correct value
as the callee is supposed to do necessary sign/zero extension.
This mismatch between bpf and x86_64 caused the incorrect results.

To resolve this problem, this patch forced caller to do needed
sign/zero extension for 8/16-bit return values as well. Note that
32-bit return values already had sign/zero extension even without
this patch.

For example, for the test case attached to this patch:

  $  cat t.c
  _Bool bar_bool(void);
  unsigned char bar_char(void);
  short bar_short(void);
  int bar_int(void);
  int foo_bool(void) {
if (bar_bool() != 1) return 0; else return 1;
  }
  int foo_char(void) {
if (bar_char() != 10) return 0; else return 1;
  }
  int foo_short(void) {
if (bar_short() != 10) return 0; else return 1;
  }
  int foo_int(void) {
if (bar_int() != 10) return 0; else return 1;
  }

Without this patch, generated call insns in IR looks like:

  %call = call zeroext i1 @bar_bool()
  %call = call zeroext i8 @bar_char()
  %call = call signext i16 @bar_short()
  %call = call i32 @bar_int()

So it is assumed that zero extension has been done for return values of
bar_bool()and bar_char(). Sign extension has been done for the return
value of bar_short(). The return value of bar_int() does not have any
assumption so caller needs to do necessary shifting to get correct
32bit values.

With this patch, generated call insns in IR looks like:

  %call = call i1 @bar_bool()
  %call = call i8 @bar_char()
  %call = call i16 @bar_short()
  %call = call i32 @bar_int()

There are no assumptions for return values of the above four function calls,
so necessary shifting is necessary for all of them.

The following is the objdump file difference for function foo_char().
Without this patch:

  0010 :
   2:   85 10 00 00 ff ff ff ff call -1
   3:   bf 01 00 00 00 00 00 00 r1 = r0
   4:   b7 00 00 00 01 00 00 00 r0 = 1
   5:   15 01 01 00 0a 00 00 00 if r1 == 10 goto +1 
   6:   b7 00 00 00 00 00 00 00 r0 = 0
  0038 :
   7:   95 00 00 00 00 00 00 00 exit

With this patch:

  0018 :
   3:   85 10 00 00 ff ff ff ff call -1
   4:   bf 01 00 00 00 00 00 00 r1 = r0
   5:   57 01 00 00 ff 00 00 00 r1 &= 255
   6:   b7 00 00 00 01 00 00 00 r0 = 1
   7:   15 01 01 00 0a 00 00 00 if r1 == 10 goto +1 
   8:   b7 00 00 00 00 00 00 00 r0 = 0
  0048 :
   9:   95 00 00 00 00 00 00 00 exit

The zero extension of the return 'char' value is done here.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131598

Files:
  clang/lib/CodeGen/TargetInfo.cpp
  clang/test/CodeGen/bpf-abiinfo.c


Index: clang/test/CodeGen/bpf-abiinfo.c
===
--- /dev/null
+++ clang/test/CodeGen/bpf-abiinfo.c
@@ -0,0 +1,24 @@
+// REQUIRES: bpf-registered-target
+// RUN: %clang_cc1 -triple bpf -O2 -emit-llvm -disable-llvm-passes %s -o - | 
FileCheck %s
+
+_Bool bar_bool(void);
+unsigned char bar_char(void);
+short bar_short(void);
+int bar_int(void);
+
+int foo_bool(void) {
+if (bar_bool() != 1) return 0; else return 1;
+}
+// CHECK: %call = call i1 @bar_bool()
+int foo_char(void) {
+if (bar_char() != 10) return 0; else return 1;
+}
+// CHECK: %call = call i8 @bar_char()
+int foo_short(void) {
+if (bar_short() != 10) return 0; else return 1;
+}
+// CHECK: %call = call i16 @bar_short()
+int foo_int(void) {
+if (bar_int() != 10) return 0; else return 1;
+}
+// CHECK: %call = call i32 @bar_int()
Index: clang/lib/CodeGen/TargetInfo.cpp
===
--- clang/lib/CodeGen/TargetInfo.cpp
+++ clang/lib/CodeGen/TargetInfo.cpp
@@ -11499,6 +11499,56 @@
 };
 } // end anonymous namespace
 
+//===--===//
+// BPF ABI Implementation
+//===--===//
+
+namespace {
+
+class BPFABIInfo : public DefaultABIInfo {
+public:
+  BPFABIInfo(CodeGenTypes ) : DefaultABIInfo(CGT) {}
+
+  ABIArgInfo classifyReturnType(QualType RetTy) const {
+if (RetTy->isVoidType())
+  return ABIArgInfo::getIgnore();
+
+if (isAggregateTypeForABI(RetTy))
+  return getNaturalAlignIndirect(RetTy);
+
+

[PATCH] D131468: [WIP][BPF]: Force sign/zero extension for arguments in callee and return values in caller

2022-08-09 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

In D131468#3708977 , @pengfei wrote:

> D124435  is going to change the assumption 
> :)

Thanks for the pointer. So looks like there is an effort to always do 
sign/zero-extension in callee. If the default is not to do extension in callee, 
we could change default value in BPF related codes, or we could implement bpf 
target specific code like this patch.

I guess I only need to focus on the func return value for now which is my focus 
any way.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D131468/new/

https://reviews.llvm.org/D131468

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D131468: [WIP][BPF]: Force truncation for arguments in callee and return values in caller

2022-08-09 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song created this revision.
yonghong-song added reviewers: ast, anakryiko.
Herald added a subscriber: pengfei.
Herald added a project: All.
yonghong-song requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

For function arguments, current implementation will do
needed sign/zero extension for 32/64-bit integer types
in callee and 8/16-bit types in caller.
This patch forced sign/zero extension to be done in
callee. Alternatively, with a slightly different
implementation, sign/zero extension can be done in caller.

Similarly for function return value, current implementation
will do needed sign/zero extension for 32/64-bit integer
types in caller and 8/16-bit return values in callee.
This patch forced sign/zero extension in caller.
Alternatively, with a slightly different implementation,
sign/zero extension can be done in callee.

  $  cat t.c
  unsigned char bar();
  char foo(short a) {
if (bar() != a) return a; else return a + 1;
  }
  $ clang -target bpf -O2 -g -c t.c
  $ llvm-objdump -S t.o
  ...
  ; char foo(short a) {
   0:   bf 16 00 00 00 00 00 00 r6 = r1
  ;   if (bar() != a) return a; else return a + 1;
   1:   bf 67 00 00 00 00 00 00 r7 = r6
   2:   67 07 00 00 30 00 00 00 r7 <<= 48
   3:   c7 07 00 00 30 00 00 00 r7 s>>= 48
   4:   85 10 00 00 ff ff ff ff call -1
   5:   bf 01 00 00 00 00 00 00 r1 = r0
   6:   57 01 00 00 ff 00 00 00 r1 &= 255
   7:   b7 00 00 00 01 00 00 00 r0 = 1
   8:   1d 71 01 00 00 00 00 00 if r1 == r7 goto +1 
   9:   b7 00 00 00 00 00 00 00 r0 = 0
  
  0050 :
  ;   if (bar() != a) return a; else return a + 1;
  10:   0f 60 00 00 00 00 00 00 r0 += r6
  ; }
  11:   95 00 00 00 00 00 00 00 exit

In the above example, 16-bit argument value has sign extension
is done in callee before it is used. 8-bit return value has
zero extension done in caller before the value is used.

Note that we mostly care about 8-bit return value sign/zero
extension in the context of bpf program calling kernel functions
since for x86_64, the caller can directly access 8-bit %al
register while bpf can only access 32/64-bit registers.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131468

Files:
  clang/lib/CodeGen/TargetInfo.cpp


Index: clang/lib/CodeGen/TargetInfo.cpp
===
--- clang/lib/CodeGen/TargetInfo.cpp
+++ clang/lib/CodeGen/TargetInfo.cpp
@@ -11498,6 +11498,75 @@
 };
 } // end anonymous namespace
 
+//===--===//
+// BPF ABI Implementation
+//===--===//
+
+namespace {
+
+class BPFABIInfo : public DefaultABIInfo {
+public:
+  BPFABIInfo(CodeGenTypes ) : DefaultABIInfo(CGT) {}
+
+#if 1
+  ABIArgInfo classifyArgumentType(QualType Ty) const {
+Ty = useFirstFieldIfTransparentUnion(Ty);
+
+if (isAggregateTypeForABI(Ty))
+  return getNaturalAlignIndirect(Ty);
+
+// Treat an enum type as its underlying type.
+if (const EnumType *EnumTy = Ty->getAs())
+  Ty = EnumTy->getDecl()->getIntegerType();
+
+ASTContext  = getContext();
+if (const auto *EIT = Ty->getAs())
+  if (EIT->getNumBits() > Context.getTypeSize(Context.Int128Ty))
+return getNaturalAlignIndirect(Ty);
+
+return ABIArgInfo::getDirect();
+  }
+#endif
+
+  ABIArgInfo classifyReturnType(QualType RetTy) const {
+if (RetTy->isVoidType())
+  return ABIArgInfo::getIgnore();
+
+if (isAggregateTypeForABI(RetTy))
+  return getNaturalAlignIndirect(RetTy);
+
+// Treat an enum type as its underlying type.
+if (const EnumType *EnumTy = RetTy->getAs())
+  RetTy = EnumTy->getDecl()->getIntegerType();
+
+ASTContext  = getContext();
+if (const auto *EIT = RetTy->getAs())
+  if (EIT->getNumBits() > Context.getTypeSize(Context.Int128Ty))
+return getNaturalAlignIndirect(RetTy);
+
+return ABIArgInfo::getDirect();
+  }
+
+  void computeInfo(CGFunctionInfo ) const override {
+FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
+for (auto  : FI.arguments())
+  I.info = classifyArgumentType(I.type);
+  }
+
+};
+
+class BPFTargetCodeGenInfo : public TargetCodeGenInfo {
+public:
+  BPFTargetCodeGenInfo(CodeGenTypes )
+  : TargetCodeGenInfo(std::make_unique(CGT)) {}
+
+  const BPFABIInfo () const {
+return static_cast(TargetCodeGenInfo::getABIInfo());
+  }
+};
+
+}
+
 
//===--===//
 // Driver code
 
//===--===//
@@ -11726,6 +11795,9 @@
   : hasFP64   ? 64
   : 32));
   }
+  case llvm::Triple::bpfeb:

[PATCH] D126839: [clang][BPF] Update comment to include TYPE_MATCH

2022-06-29 Thread Yonghong Song via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG70557eb3938a: [clang][BPF] Update comment to include 
TYPE_MATCH (authored by d-e-s-o, committed by yonghong-song).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126839/new/

https://reviews.llvm.org/D126839

Files:
  clang/lib/Sema/SemaChecking.cpp


Index: clang/lib/Sema/SemaChecking.cpp
===
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -3254,7 +3254,7 @@
   if (ArgType->getAsPlaceholderType())
 return false;
 
-  // for TYPE_EXISTENCE/TYPE_SIZEOF reloc type
+  // for TYPE_EXISTENCE/TYPE_MATCH/TYPE_SIZEOF reloc type
   // format:
   //   1. __builtin_preserve_type_info(*( *)0, flag);
   //   2.  var;


Index: clang/lib/Sema/SemaChecking.cpp
===
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -3254,7 +3254,7 @@
   if (ArgType->getAsPlaceholderType())
 return false;
 
-  // for TYPE_EXISTENCE/TYPE_SIZEOF reloc type
+  // for TYPE_EXISTENCE/TYPE_MATCH/TYPE_SIZEOF reloc type
   // format:
   //   1. __builtin_preserve_type_info(*( *)0, flag);
   //   2.  var;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D126839: [clang][BPF] Update comment to include TYPE_MATCH

2022-06-02 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

In D126839#3554474 , @d-e-s-o wrote:

> In D126839#3554125 , @yonghong-song 
> wrote:
>
>> Also let us merge it unti kernel side change is in reasonable shape.
>
> From what I understand it may take several days or some other not-precisely 
> specified time until "nightly" builds, which we use in the BPF CI, pick up 
> the change. Hence, I was under the impression that we should attempt to merge 
> it sooner rather than later once it's ready to be merged. The kernel changes 
> are in good enough shape to show feasibility, so I don't really see why not, 
> but it's your call.

We can merge after the new mechanism is acked (not patch getting merged) in 
kernel review.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126839/new/

https://reviews.llvm.org/D126839

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D126839: [clang][BPF] Update comment to include TYPE_MATCH

2022-06-02 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

LGTM. But add the reference (https://reviews.llvm.org/D126838) to the commit 
message to indicate which 'earlier change'. Also let us merge it unti kernel 
side change is in reasonable shape.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126839/new/

https://reviews.llvm.org/D126839

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D126223: [Clang][Sema] Fix a SemaType/VisitTagTypeLoc assertion

2022-05-24 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song abandoned this revision.
yonghong-song added a comment.

@compnerd provided a better fix https://reviews.llvm.org/D126093, so I am going 
to close this one.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126223/new/

https://reviews.llvm.org/D126223

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D126093: Sema: adjust assertion to account for deduced types

2022-05-24 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song accepted this revision.
yonghong-song added a comment.
This revision is now accepted and ready to land.

Sorry. I missed this patch somehow and did another patch 
https://reviews.llvm.org/D126223. My patch skipped 
`TL.setNameLoc(DS.getTypeSpecTypeNameLoc());` which probably not correct. 
Accept this patch and abandon my patch D126223 
.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126093/new/

https://reviews.llvm.org/D126093

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D126223: [Clang][Sema] Fix a SemaType/VisitTagTypeLoc assertion

2022-05-23 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song created this revision.
yonghong-song added reviewers: compnerd, aaron.ballman.
Herald added a project: All.
yonghong-song requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Patch [1] added btf_type_tag attribute support. In particular,
in class TypeSpecLocFiller, it permitted to visit pointee's
of a PointerTypeLoc. This caused a behavior change outside
of the btf_type_tag area. Saleem Abdulrasool reported that
assertion would happen for the following code:

struct dispatch_object_s;
 void *_dispatch_queue_get_head(struct dispatch_object_s * volatile 
dq_items_head) {

  return (void *)(_Atomic __typeof__(dq_items_head) *)dq_items_head;

}

The following is what happened:

  ...   
  1. In Parse/ParseDecl.cpp, ParseTypeofSpecifier() is called to handle typeof
 specifier. DeclSpec is set to TST_typeofExpr.
  2. Later, in Sema/SemaType.cpp,
 TypeSpecLocFiller(S, S.Context, State, D.getDeclSpec()).Visit(CurrTL)
 is called.
  3. The specific types visited in the following order:
 VisitAtomicTypeLoc()
 VisitPointerTypeLoc()
 VisitElaboratedTypeLo()c
 VisitTagTypeLoc()

During VisitTagTypeLoc(), since DeclSpec is TST_typeofExpr, this will cause
assertion error in DS.getTypeSpecTypeNameLoc().

  clang: /home/yhs/work/llvm-project/clang/include/clang/Sema/DeclSpec.h:519:
clang::SourceLocation clang::DeclSpec::getTypeSpecTypeNameLoc() const:
Assertion `isDeclRep((TST) TypeSpecType) || TypeSpecType == TST_typename' 
failed.

If we remove the _Atomic in the above code like

struct dispatch_object_s;
 void *_dispatch_queue_get_head(struct dispatch_object_s * volatile 
dq_items_head) {

  return (void *)(_Atomic __typeof__(dq_items_head) *)dq_items_head;

}

The above step 3 will have VisitTypeOfExprTypeLoc() and no assertion happens.

To fix the issue, in VisitTagTypeLoc(), let us not do 
DS.getTypeSpecTypeNameLoc()
if the DeclSpec is TST_typeofExpr.

  [1] https://reviews.llvm.org/D99


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D126223

Files:
  clang/lib/Sema/SemaType.cpp
  clang/test/Sema/atomic-typeof.c


Index: clang/test/Sema/atomic-typeof.c
===
--- /dev/null
+++ clang/test/Sema/atomic-typeof.c
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c11 -x c %s
+
+struct dispatch_object_s;
+void _dispatch_queue_get_head(struct dispatch_object_s * volatile 
dq_items_head) {
+  (_Atomic __typeof__(dq_items_head) *)dq_items_head;
+}
Index: clang/lib/Sema/SemaType.cpp
===
--- clang/lib/Sema/SemaType.cpp
+++ clang/lib/Sema/SemaType.cpp
@@ -6109,7 +6109,8 @@
 TL.setArgLocInfo(I, TemplateArgsInfo.arguments()[I].getLocInfo());
 }
 void VisitTagTypeLoc(TagTypeLoc TL) {
-  TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
+  if (DS.getTypeSpecType() != DeclSpec::TST_typeofExpr)
+TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
 }
 void VisitAtomicTypeLoc(AtomicTypeLoc TL) {
   // An AtomicTypeLoc can come from either an _Atomic(...) type specifier


Index: clang/test/Sema/atomic-typeof.c
===
--- /dev/null
+++ clang/test/Sema/atomic-typeof.c
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c11 -x c %s
+
+struct dispatch_object_s;
+void _dispatch_queue_get_head(struct dispatch_object_s * volatile dq_items_head) {
+  (_Atomic __typeof__(dq_items_head) *)dq_items_head;
+}
Index: clang/lib/Sema/SemaType.cpp
===
--- clang/lib/Sema/SemaType.cpp
+++ clang/lib/Sema/SemaType.cpp
@@ -6109,7 +6109,8 @@
 TL.setArgLocInfo(I, TemplateArgsInfo.arguments()[I].getLocInfo());
 }
 void VisitTagTypeLoc(TagTypeLoc TL) {
-  TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
+  if (DS.getTypeSpecType() != DeclSpec::TST_typeofExpr)
+TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
 }
 void VisitAtomicTypeLoc(AtomicTypeLoc TL) {
   // An AtomicTypeLoc can come from either an _Atomic(...) type specifier
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D111199: [Clang][LLVM][Attr] support btf_type_tag attribute

2022-05-20 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

@compnerd Thanks for reporting. I will take a look as soon as possible.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D99/new/

https://reviews.llvm.org/D99

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D124813: [HLSL] Add clang builtin for HLSL.

2022-05-04 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song resigned from this revision.
yonghong-song added a comment.

please having proper reviewers. Me, @anakryiko  @ast are not the right persons 
to review this patch.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D124813/new/

https://reviews.llvm.org/D124813

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D122271: [Clang] -Wunused-but-set-variable warning - handle also pre/post unary operators

2022-03-22 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

LGTM. Maybe @efriedma can look at this as well.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122271/new/

https://reviews.llvm.org/D122271

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D121715: [Clang] Fix an unused-but-set-variable warning with volatile variable

2022-03-21 Thread Yonghong Song via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGbdf69f63df2c: [Clang] Fix an unused-but-set-variable warning 
with volatile variable (authored by yonghong-song).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D121715/new/

https://reviews.llvm.org/D121715

Files:
  clang/lib/Sema/SemaExprCXX.cpp
  clang/test/Sema/warn-unused-but-set-variables.c


Index: clang/test/Sema/warn-unused-but-set-variables.c
===
--- clang/test/Sema/warn-unused-but-set-variables.c
+++ clang/test/Sema/warn-unused-but-set-variables.c
@@ -23,10 +23,24 @@
   int a;
   w = (a = 0);
 
+  int j = 0; // expected-warning{{variable 'j' set but not used}}
+  for (int i = 0; i < 1000; i++)
+j += 1;
+
   // Following gcc, warn for a volatile variable.
   volatile int b; // expected-warning{{variable 'b' set but not used}}
   b = 0;
 
+  // volatile variable k is used, no warning.
+  volatile int k = 0;
+  for (int i = 0; i < 1000; i++)
+k += 1;
+
+  // typedef of volatile type, no warning.
+  typedef volatile int volint;
+  volint l = 0;
+  l += 1;
+
   int x;
   x = 0;
   return x;
Index: clang/lib/Sema/SemaExprCXX.cpp
===
--- clang/lib/Sema/SemaExprCXX.cpp
+++ clang/lib/Sema/SemaExprCXX.cpp
@@ -7925,6 +7925,7 @@
 static void MaybeDecrementCount(
 Expr *E, llvm::DenseMap ) {
   DeclRefExpr *LHS = nullptr;
+  bool IsCompoundAssign = false;
   if (BinaryOperator *BO = dyn_cast(E)) {
 if (BO->getLHS()->getType()->isDependentType() ||
 BO->getRHS()->getType()->isDependentType()) {
@@ -7932,6 +7933,8 @@
 return;
 } else if (!BO->isAssignmentOp())
   return;
+else
+  IsCompoundAssign = BO->isCompoundAssignmentOp();
 LHS = dyn_cast(BO->getLHS());
   } else if (CXXOperatorCallExpr *COCE = dyn_cast(E)) {
 if (COCE->getOperator() != OO_Equal)
@@ -7943,6 +7946,10 @@
   VarDecl *VD = dyn_cast(LHS->getDecl());
   if (!VD)
 return;
+  // Don't decrement RefsMinusAssignments if volatile variable with compound
+  // assignment (+=, ...) to avoid potential unused-but-set-variable warning.
+  if (IsCompoundAssign && VD->getType().isVolatileQualified())
+return;
   auto iter = RefsMinusAssignments.find(VD);
   if (iter == RefsMinusAssignments.end())
 return;


Index: clang/test/Sema/warn-unused-but-set-variables.c
===
--- clang/test/Sema/warn-unused-but-set-variables.c
+++ clang/test/Sema/warn-unused-but-set-variables.c
@@ -23,10 +23,24 @@
   int a;
   w = (a = 0);
 
+  int j = 0; // expected-warning{{variable 'j' set but not used}}
+  for (int i = 0; i < 1000; i++)
+j += 1;
+
   // Following gcc, warn for a volatile variable.
   volatile int b; // expected-warning{{variable 'b' set but not used}}
   b = 0;
 
+  // volatile variable k is used, no warning.
+  volatile int k = 0;
+  for (int i = 0; i < 1000; i++)
+k += 1;
+
+  // typedef of volatile type, no warning.
+  typedef volatile int volint;
+  volint l = 0;
+  l += 1;
+
   int x;
   x = 0;
   return x;
Index: clang/lib/Sema/SemaExprCXX.cpp
===
--- clang/lib/Sema/SemaExprCXX.cpp
+++ clang/lib/Sema/SemaExprCXX.cpp
@@ -7925,6 +7925,7 @@
 static void MaybeDecrementCount(
 Expr *E, llvm::DenseMap ) {
   DeclRefExpr *LHS = nullptr;
+  bool IsCompoundAssign = false;
   if (BinaryOperator *BO = dyn_cast(E)) {
 if (BO->getLHS()->getType()->isDependentType() ||
 BO->getRHS()->getType()->isDependentType()) {
@@ -7932,6 +7933,8 @@
 return;
 } else if (!BO->isAssignmentOp())
   return;
+else
+  IsCompoundAssign = BO->isCompoundAssignmentOp();
 LHS = dyn_cast(BO->getLHS());
   } else if (CXXOperatorCallExpr *COCE = dyn_cast(E)) {
 if (COCE->getOperator() != OO_Equal)
@@ -7943,6 +7946,10 @@
   VarDecl *VD = dyn_cast(LHS->getDecl());
   if (!VD)
 return;
+  // Don't decrement RefsMinusAssignments if volatile variable with compound
+  // assignment (+=, ...) to avoid potential unused-but-set-variable warning.
+  if (IsCompoundAssign && VD->getType().isVolatileQualified())
+return;
   auto iter = RefsMinusAssignments.find(VD);
   if (iter == RefsMinusAssignments.end())
 return;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D121715: [Clang] Fix an unused-but-set-variable warning with volatile variable

2022-03-21 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 417060.
yonghong-song added a comment.

- fix typo
- add a new test case with typedef of volatile type.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D121715/new/

https://reviews.llvm.org/D121715

Files:
  clang/lib/Sema/SemaExprCXX.cpp
  clang/test/Sema/warn-unused-but-set-variables.c


Index: clang/test/Sema/warn-unused-but-set-variables.c
===
--- clang/test/Sema/warn-unused-but-set-variables.c
+++ clang/test/Sema/warn-unused-but-set-variables.c
@@ -23,10 +23,24 @@
   int a;
   w = (a = 0);
 
+  int j = 0; // expected-warning{{variable 'j' set but not used}}
+  for (int i = 0; i < 1000; i++)
+j += 1;
+
   // Following gcc, warn for a volatile variable.
   volatile int b; // expected-warning{{variable 'b' set but not used}}
   b = 0;
 
+  // volatile variable k is used, no warning.
+  volatile int k = 0;
+  for (int i = 0; i < 1000; i++)
+k += 1;
+
+  // typedef of volatile type, no warning.
+  typedef volatile int volint;
+  volint l = 0;
+  l += 1;
+
   int x;
   x = 0;
   return x;
Index: clang/lib/Sema/SemaExprCXX.cpp
===
--- clang/lib/Sema/SemaExprCXX.cpp
+++ clang/lib/Sema/SemaExprCXX.cpp
@@ -7925,6 +7925,7 @@
 static void MaybeDecrementCount(
 Expr *E, llvm::DenseMap ) {
   DeclRefExpr *LHS = nullptr;
+  bool IsCompoundAssign = false;
   if (BinaryOperator *BO = dyn_cast(E)) {
 if (BO->getLHS()->getType()->isDependentType() ||
 BO->getRHS()->getType()->isDependentType()) {
@@ -7932,6 +7933,8 @@
 return;
 } else if (!BO->isAssignmentOp())
   return;
+else
+  IsCompoundAssign = BO->isCompoundAssignmentOp();
 LHS = dyn_cast(BO->getLHS());
   } else if (CXXOperatorCallExpr *COCE = dyn_cast(E)) {
 if (COCE->getOperator() != OO_Equal)
@@ -7943,6 +7946,10 @@
   VarDecl *VD = dyn_cast(LHS->getDecl());
   if (!VD)
 return;
+  // Don't decrement RefsMinusAssignments if volatile variable with compound
+  // assignment (+=, ...) to avoid potential unused-but-set-variable warning.
+  if (IsCompoundAssign && VD->getType().isVolatileQualified())
+return;
   auto iter = RefsMinusAssignments.find(VD);
   if (iter == RefsMinusAssignments.end())
 return;


Index: clang/test/Sema/warn-unused-but-set-variables.c
===
--- clang/test/Sema/warn-unused-but-set-variables.c
+++ clang/test/Sema/warn-unused-but-set-variables.c
@@ -23,10 +23,24 @@
   int a;
   w = (a = 0);
 
+  int j = 0; // expected-warning{{variable 'j' set but not used}}
+  for (int i = 0; i < 1000; i++)
+j += 1;
+
   // Following gcc, warn for a volatile variable.
   volatile int b; // expected-warning{{variable 'b' set but not used}}
   b = 0;
 
+  // volatile variable k is used, no warning.
+  volatile int k = 0;
+  for (int i = 0; i < 1000; i++)
+k += 1;
+
+  // typedef of volatile type, no warning.
+  typedef volatile int volint;
+  volint l = 0;
+  l += 1;
+
   int x;
   x = 0;
   return x;
Index: clang/lib/Sema/SemaExprCXX.cpp
===
--- clang/lib/Sema/SemaExprCXX.cpp
+++ clang/lib/Sema/SemaExprCXX.cpp
@@ -7925,6 +7925,7 @@
 static void MaybeDecrementCount(
 Expr *E, llvm::DenseMap ) {
   DeclRefExpr *LHS = nullptr;
+  bool IsCompoundAssign = false;
   if (BinaryOperator *BO = dyn_cast(E)) {
 if (BO->getLHS()->getType()->isDependentType() ||
 BO->getRHS()->getType()->isDependentType()) {
@@ -7932,6 +7933,8 @@
 return;
 } else if (!BO->isAssignmentOp())
   return;
+else
+  IsCompoundAssign = BO->isCompoundAssignmentOp();
 LHS = dyn_cast(BO->getLHS());
   } else if (CXXOperatorCallExpr *COCE = dyn_cast(E)) {
 if (COCE->getOperator() != OO_Equal)
@@ -7943,6 +7946,10 @@
   VarDecl *VD = dyn_cast(LHS->getDecl());
   if (!VD)
 return;
+  // Don't decrement RefsMinusAssignments if volatile variable with compound
+  // assignment (+=, ...) to avoid potential unused-but-set-variable warning.
+  if (IsCompoundAssign && VD->getType().isVolatileQualified())
+return;
   auto iter = RefsMinusAssignments.find(VD);
   if (iter == RefsMinusAssignments.end())
 return;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D121715: [Clang] Fix an unused-but-set-variable warning with volatile variable

2022-03-21 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

In D121715#3397407 , @efriedma wrote:

> In D121715#3397215 , @xbolva00 
> wrote:
>
>> If there is j = j + 1 .. do we warn ?
>
>
>
> In D121715#3397237 , @xbolva00 
> wrote:
>
>>   j++;
>>
>> Should not we warn in this case? (Currently no warning)
>
> We probably should warn in both those cases; the variable clearly isn't 
> "used" in any meaningful way.

The 'j++' is not warned as it is not a BinaryOperator or CXXOperatorCallExpr so 
it is not handled and considered no warning by default.
This patch intends to handle volatile variables. We can have another patch to 
deal with this specific case.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D121715/new/

https://reviews.llvm.org/D121715

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D121715: [Clang] Fix an unused-but-set-variable warning with volatile variable

2022-03-21 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added inline comments.



Comment at: clang/lib/Sema/SemaDecl.cpp:2033
 return;
+
   unsigned DiagID = isa(VD) ? diag::warn_unused_but_set_parameter

efriedma wrote:
> (Accidental change?)
Right. Thanks for catching this. This is a leftover from my previous debugging 
code and forgot to remove it.



Comment at: clang/lib/Sema/SemaExprCXX.cpp:7925
   DeclRefExpr *LHS = nullptr;
+  bool IsComponentAssign = false;
   if (BinaryOperator *BO = dyn_cast(E)) {

aaron.ballman wrote:
> 
Thanks, really embarrassing typo. 



Comment at: clang/test/Sema/warn-unused-but-set-variables.c:35
+  // volatile variable k is used, no warning.
+  volatile int k = 0;
+  for (int i = 0; i < 1000; i++)

aaron.ballman wrote:
> I'd like to see another test:
> ```
> typedef volatile int volint;
> volint l = 0;
> l += 1; // no warning
> ```
Will do.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D121715/new/

https://reviews.llvm.org/D121715

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120296: [Attr] Fix a btf_type_tag AST generation bug

2022-03-16 Thread Yonghong Song via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG3251ba2d0fcf: [Attr] Fix a btf_type_tag AST generation 
(authored by yonghong-song).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120296/new/

https://reviews.llvm.org/D120296

Files:
  clang/include/clang-c/Index.h
  clang/include/clang/AST/ASTContext.h
  clang/include/clang/AST/ASTNodeTraverser.h
  clang/include/clang/AST/PropertiesBase.td
  clang/include/clang/AST/RecursiveASTVisitor.h
  clang/include/clang/AST/Type.h
  clang/include/clang/AST/TypeLoc.h
  clang/include/clang/AST/TypeProperties.td
  clang/include/clang/Basic/TypeNodes.td
  clang/include/clang/Serialization/ASTRecordReader.h
  clang/include/clang/Serialization/ASTRecordWriter.h
  clang/include/clang/Serialization/TypeBitCodes.def
  clang/lib/AST/ASTContext.cpp
  clang/lib/AST/ASTStructuralEquivalence.cpp
  clang/lib/AST/ItaniumMangle.cpp
  clang/lib/AST/TypeLoc.cpp
  clang/lib/AST/TypePrinter.cpp
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/CodeGen/CGDebugInfo.h
  clang/lib/CodeGen/CodeGenFunction.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaType.cpp
  clang/lib/Sema/TreeTransform.h
  clang/lib/Serialization/ASTReader.cpp
  clang/lib/Serialization/ASTWriter.cpp
  clang/test/CodeGen/attr-btf_type_tag-similar-type.c
  clang/test/PCH/btf_type_tag_attr.c
  clang/test/Sema/attr-btf_type_tag.c
  clang/tools/libclang/CIndex.cpp
  clang/tools/libclang/CXType.cpp

Index: clang/tools/libclang/CXType.cpp
===
--- clang/tools/libclang/CXType.cpp
+++ clang/tools/libclang/CXType.cpp
@@ -116,6 +116,7 @@
 TKCASE(Elaborated);
 TKCASE(Pipe);
 TKCASE(Attributed);
+TKCASE(BTFTagAttributed);
 TKCASE(Atomic);
 default:
   return CXType_Unexposed;
@@ -136,6 +137,10 @@
 return MakeCXType(ATT->getEquivalentType(), TU);
   }
 }
+if (auto *ATT = T->getAs()) {
+  if (!(TU->ParsingOptions & CXTranslationUnit_IncludeAttributedTypes))
+return MakeCXType(ATT->getWrappedType(), TU);
+}
 // Handle paren types as the original type
 if (auto *PTT = T->getAs()) {
   return MakeCXType(PTT->getInnerType(), TU);
@@ -610,6 +615,7 @@
 TKIND(Elaborated);
 TKIND(Pipe);
 TKIND(Attributed);
+TKIND(BTFTagAttributed);
 TKIND(BFloat16);
 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) TKIND(Id);
 #include "clang/Basic/OpenCLImageTypes.def"
@@ -1051,6 +1057,9 @@
   if (auto *ATT = T->getAs())
 return MakeCXType(ATT->getModifiedType(), GetTU(CT));
 
+  if (auto *ATT = T->getAs())
+return MakeCXType(ATT->getWrappedType(), GetTU(CT));
+
   return MakeCXType(QualType(), GetTU(CT));
 }
 
Index: clang/tools/libclang/CIndex.cpp
===
--- clang/tools/libclang/CIndex.cpp
+++ clang/tools/libclang/CIndex.cpp
@@ -1672,6 +1672,10 @@
   return Visit(TL.getModifiedLoc());
 }
 
+bool CursorVisitor::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
+  return Visit(TL.getWrappedLoc());
+}
+
 bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL,
  bool SkipResultType) {
   if (!SkipResultType && Visit(TL.getReturnLoc()))
Index: clang/test/Sema/attr-btf_type_tag.c
===
--- clang/test/Sema/attr-btf_type_tag.c
+++ clang/test/Sema/attr-btf_type_tag.c
@@ -23,3 +23,19 @@
 int __tag4 * __tag5 * __tag6 *foo1(struct t __tag1 * __tag2 * __tag3 *a1) {
   return (int __tag4 * __tag5 * __tag6 *)a1[0][0]->d;
 }
+
+// The btf_type_tag attribute will be ignored during _Generic type matching
+int g1 = _Generic((int *)0, int __tag1 *: 0);
+int g2 = _Generic((int __tag1 *)0, int *: 0);
+int g3 = _Generic(0,
+  int __tag1 * : 0, // expected-note {{compatible type 'int  btf_type_tag(tag1)*' (aka 'int *') specified here}}
+  int * : 0, // expected-error {{type 'int *' in generic association compatible with previously specified type 'int  btf_type_tag(tag1)*' (aka 'int *')}}
+  default : 0);
+
+// The btf_type_tag attribute will be ignored during overloadable type matching
+void bar2(int __tag1 *a) __attribute__((overloadable)) { asm volatile (""); } // expected-note {{previous definition is here}}
+void bar2(int *a) __attribute__((overloadable)) { asm volatile (""); } // expected-error {{redefinition of 'bar2'}}
+void foo2(int __tag1 *a, int *b) {
+  bar2(a);
+  bar2(b);
+}
Index: clang/test/PCH/btf_type_tag_attr.c
===
--- /dev/null
+++ clang/test/PCH/btf_type_tag_attr.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -emit-pch -o %t %s
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -include-pch %t \
+// RUN:   -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+int 

[PATCH] D121715: [Clang] Fix an unused-but-set-variable warning with volatile variable

2022-03-15 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added inline comments.



Comment at: clang/test/Sema/warn-unused-but-set-variables.c:66
+  for (int i = 0; i < 1000; i++)
+j += 1;
+  return;

efriedma wrote:
> The handling of this testcase without your patch seems fine.  Even if the 
> value of j is technically read, it's not "used" in any meaningful way.
> 
> I think the distinguishing factor for the testcase we discussed before is the 
> use of a "volatile".  Volatile is rare, and indicates the user is 
> intentionally doing something unusual, so it probably doesn't make sense to 
> warn.
@eli.friedman Just updated the patch to handle volatile variable only.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D121715/new/

https://reviews.llvm.org/D121715

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D121715: [Clang] Fix an unused-but-set-variable warning with volatile variable

2022-03-15 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 415665.
yonghong-song retitled this revision from "[Clang] Fix an 
unused-but-set-variable warning with compond assignment" to "[Clang] Fix an 
unused-but-set-variable warning with volatile variable".
yonghong-song edited the summary of this revision.
yonghong-song added a comment.

- only handle volatile variables with component assignment.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D121715/new/

https://reviews.llvm.org/D121715

Files:
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaExprCXX.cpp
  clang/test/Sema/warn-unused-but-set-variables.c


Index: clang/test/Sema/warn-unused-but-set-variables.c
===
--- clang/test/Sema/warn-unused-but-set-variables.c
+++ clang/test/Sema/warn-unused-but-set-variables.c
@@ -23,10 +23,19 @@
   int a;
   w = (a = 0);
 
+  int j = 0; // expected-warning{{variable 'j' set but not used}}
+  for (int i = 0; i < 1000; i++)
+j += 1;
+
   // Following gcc, warn for a volatile variable.
   volatile int b; // expected-warning{{variable 'b' set but not used}}
   b = 0;
 
+  // volatile variable k is used, no warning.
+  volatile int k = 0;
+  for (int i = 0; i < 1000; i++)
+k += 1;
+
   int x;
   x = 0;
   return x;
Index: clang/lib/Sema/SemaExprCXX.cpp
===
--- clang/lib/Sema/SemaExprCXX.cpp
+++ clang/lib/Sema/SemaExprCXX.cpp
@@ -7922,6 +7922,7 @@
 static void MaybeDecrementCount(
 Expr *E, llvm::DenseMap ) {
   DeclRefExpr *LHS = nullptr;
+  bool IsComponentAssign = false;
   if (BinaryOperator *BO = dyn_cast(E)) {
 if (BO->getLHS()->getType()->isDependentType() ||
 BO->getRHS()->getType()->isDependentType()) {
@@ -7929,6 +7930,8 @@
 return;
 } else if (!BO->isAssignmentOp())
   return;
+else
+  IsComponentAssign = BO->isCompoundAssignmentOp();
 LHS = dyn_cast(BO->getLHS());
   } else if (CXXOperatorCallExpr *COCE = dyn_cast(E)) {
 if (COCE->getOperator() != OO_Equal)
@@ -7940,6 +7943,10 @@
   VarDecl *VD = dyn_cast(LHS->getDecl());
   if (!VD)
 return;
+  // Don't decrement RefsMinusAssignments if volatile variable with component
+  // assignment (+=, ...) to avoid potential unused-but-set-variable warning.
+  if (IsComponentAssign && VD->getType().isVolatileQualified())
+return;
   auto iter = RefsMinusAssignments.find(VD);
   if (iter == RefsMinusAssignments.end())
 return;
Index: clang/lib/Sema/SemaDecl.cpp
===
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -2030,6 +2030,7 @@
  "Found a negative number of references to a VarDecl");
   if (iter->getSecond() != 0)
 return;
+
   unsigned DiagID = isa(VD) ? diag::warn_unused_but_set_parameter
  : diag::warn_unused_but_set_variable;
   Diag(VD->getLocation(), DiagID) << VD;


Index: clang/test/Sema/warn-unused-but-set-variables.c
===
--- clang/test/Sema/warn-unused-but-set-variables.c
+++ clang/test/Sema/warn-unused-but-set-variables.c
@@ -23,10 +23,19 @@
   int a;
   w = (a = 0);
 
+  int j = 0; // expected-warning{{variable 'j' set but not used}}
+  for (int i = 0; i < 1000; i++)
+j += 1;
+
   // Following gcc, warn for a volatile variable.
   volatile int b; // expected-warning{{variable 'b' set but not used}}
   b = 0;
 
+  // volatile variable k is used, no warning.
+  volatile int k = 0;
+  for (int i = 0; i < 1000; i++)
+k += 1;
+
   int x;
   x = 0;
   return x;
Index: clang/lib/Sema/SemaExprCXX.cpp
===
--- clang/lib/Sema/SemaExprCXX.cpp
+++ clang/lib/Sema/SemaExprCXX.cpp
@@ -7922,6 +7922,7 @@
 static void MaybeDecrementCount(
 Expr *E, llvm::DenseMap ) {
   DeclRefExpr *LHS = nullptr;
+  bool IsComponentAssign = false;
   if (BinaryOperator *BO = dyn_cast(E)) {
 if (BO->getLHS()->getType()->isDependentType() ||
 BO->getRHS()->getType()->isDependentType()) {
@@ -7929,6 +7930,8 @@
 return;
 } else if (!BO->isAssignmentOp())
   return;
+else
+  IsComponentAssign = BO->isCompoundAssignmentOp();
 LHS = dyn_cast(BO->getLHS());
   } else if (CXXOperatorCallExpr *COCE = dyn_cast(E)) {
 if (COCE->getOperator() != OO_Equal)
@@ -7940,6 +7943,10 @@
   VarDecl *VD = dyn_cast(LHS->getDecl());
   if (!VD)
 return;
+  // Don't decrement RefsMinusAssignments if volatile variable with component
+  // assignment (+=, ...) to avoid potential unused-but-set-variable warning.
+  if (IsComponentAssign && VD->getType().isVolatileQualified())
+return;
   auto iter = RefsMinusAssignments.find(VD);
   if (iter == RefsMinusAssignments.end())
 return;
Index: clang/lib/Sema/SemaDecl.cpp

[PATCH] D120296: [Attr] Fix a btf_type_tag AST generation bug

2022-03-15 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 415638.
yonghong-song added a comment.

- change TransformBTFTagAttributedType() implementation with a simple 
llvm_unreachable() message. I tested linux kernel and its selftests/bpf 
compilation with clang and found the only TreeTransformation function is called 
with TransformAutoType() due to __auto_type usage.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120296/new/

https://reviews.llvm.org/D120296

Files:
  clang/include/clang-c/Index.h
  clang/include/clang/AST/ASTContext.h
  clang/include/clang/AST/ASTNodeTraverser.h
  clang/include/clang/AST/PropertiesBase.td
  clang/include/clang/AST/RecursiveASTVisitor.h
  clang/include/clang/AST/Type.h
  clang/include/clang/AST/TypeLoc.h
  clang/include/clang/AST/TypeProperties.td
  clang/include/clang/Basic/TypeNodes.td
  clang/include/clang/Serialization/ASTRecordReader.h
  clang/include/clang/Serialization/ASTRecordWriter.h
  clang/include/clang/Serialization/TypeBitCodes.def
  clang/lib/AST/ASTContext.cpp
  clang/lib/AST/ASTStructuralEquivalence.cpp
  clang/lib/AST/ItaniumMangle.cpp
  clang/lib/AST/TypeLoc.cpp
  clang/lib/AST/TypePrinter.cpp
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/CodeGen/CGDebugInfo.h
  clang/lib/CodeGen/CodeGenFunction.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaType.cpp
  clang/lib/Sema/TreeTransform.h
  clang/lib/Serialization/ASTReader.cpp
  clang/lib/Serialization/ASTWriter.cpp
  clang/test/CodeGen/attr-btf_type_tag-similar-type.c
  clang/test/PCH/btf_type_tag_attr.c
  clang/test/Sema/attr-btf_type_tag.c
  clang/tools/libclang/CIndex.cpp
  clang/tools/libclang/CXType.cpp

Index: clang/tools/libclang/CXType.cpp
===
--- clang/tools/libclang/CXType.cpp
+++ clang/tools/libclang/CXType.cpp
@@ -116,6 +116,7 @@
 TKCASE(Elaborated);
 TKCASE(Pipe);
 TKCASE(Attributed);
+TKCASE(BTFTagAttributed);
 TKCASE(Atomic);
 default:
   return CXType_Unexposed;
@@ -136,6 +137,10 @@
 return MakeCXType(ATT->getEquivalentType(), TU);
   }
 }
+if (auto *ATT = T->getAs()) {
+  if (!(TU->ParsingOptions & CXTranslationUnit_IncludeAttributedTypes))
+return MakeCXType(ATT->getWrappedType(), TU);
+}
 // Handle paren types as the original type
 if (auto *PTT = T->getAs()) {
   return MakeCXType(PTT->getInnerType(), TU);
@@ -610,6 +615,7 @@
 TKIND(Elaborated);
 TKIND(Pipe);
 TKIND(Attributed);
+TKIND(BTFTagAttributed);
 TKIND(BFloat16);
 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) TKIND(Id);
 #include "clang/Basic/OpenCLImageTypes.def"
@@ -1051,6 +1057,9 @@
   if (auto *ATT = T->getAs())
 return MakeCXType(ATT->getModifiedType(), GetTU(CT));
 
+  if (auto *ATT = T->getAs())
+return MakeCXType(ATT->getWrappedType(), GetTU(CT));
+
   return MakeCXType(QualType(), GetTU(CT));
 }
 
Index: clang/tools/libclang/CIndex.cpp
===
--- clang/tools/libclang/CIndex.cpp
+++ clang/tools/libclang/CIndex.cpp
@@ -1672,6 +1672,10 @@
   return Visit(TL.getModifiedLoc());
 }
 
+bool CursorVisitor::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
+  return Visit(TL.getWrappedLoc());
+}
+
 bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL,
  bool SkipResultType) {
   if (!SkipResultType && Visit(TL.getReturnLoc()))
Index: clang/test/Sema/attr-btf_type_tag.c
===
--- clang/test/Sema/attr-btf_type_tag.c
+++ clang/test/Sema/attr-btf_type_tag.c
@@ -23,3 +23,19 @@
 int __tag4 * __tag5 * __tag6 *foo1(struct t __tag1 * __tag2 * __tag3 *a1) {
   return (int __tag4 * __tag5 * __tag6 *)a1[0][0]->d;
 }
+
+// The btf_type_tag attribute will be ignored during _Generic type matching
+int g1 = _Generic((int *)0, int __tag1 *: 0);
+int g2 = _Generic((int __tag1 *)0, int *: 0);
+int g3 = _Generic(0,
+  int __tag1 * : 0, // expected-note {{compatible type 'int  btf_type_tag(tag1)*' (aka 'int *') specified here}}
+  int * : 0, // expected-error {{type 'int *' in generic association compatible with previously specified type 'int  btf_type_tag(tag1)*' (aka 'int *')}}
+  default : 0);
+
+// The btf_type_tag attribute will be ignored during overloadable type matching
+void bar2(int __tag1 *a) __attribute__((overloadable)) { asm volatile (""); } // expected-note {{previous definition is here}}
+void bar2(int *a) __attribute__((overloadable)) { asm volatile (""); } // expected-error {{redefinition of 'bar2'}}
+void foo2(int __tag1 *a, int *b) {
+  bar2(a);
+  bar2(b);
+}
Index: clang/test/PCH/btf_type_tag_attr.c
===
--- /dev/null
+++ clang/test/PCH/btf_type_tag_attr.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple aarch64-linux-gnu 

[PATCH] D120296: [Attr] Fix a btf_type_tag AST generation bug

2022-03-15 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added inline comments.



Comment at: clang/lib/Sema/TreeTransform.h:6872
+  const BTFTagAttributedType *oldType = TL.getTypePtr();
+  StringRef Tag = oldType->getTag();
+  QualType modifiedType = getDerived().TransformType(TLB, TL.getModifiedLoc());

aaron.ballman wrote:
> aaron.ballman wrote:
> > yonghong-song wrote:
> > > erichkeane wrote:
> > > > Most of this tree-transform doesn't really do much, since this is a 'C' 
> > > > only type, but otherwise we'd probably want to allow the tag itself to 
> > > > be dependent.  
> > > > 
> > > > We still need this though, since there are other non-template 
> > > > tree-transforms.
> > > > 
> > > > You also might need to make this not contain a `StringRef` based on the 
> > > > serialization issues above.
> > > I will try to do some experiment and simplify this. Indeed this is C and 
> > > non templates are involved.
> > > We still need this though, since there are other non-template 
> > > tree-transforms.
> > 
> > Are we sure any of them can be hit for this new type? It'd be nice to keep 
> > this out of the template instantiation bits if possible.
> I think this may be the only unresolved conversation in the review.
> Are we sure any of them can be hit for this new type? It'd be nice to keep 
> this out of the
> template instantiation bits if possible.

Actually I am not sure. But looks like we have to have the function implemented 
due to
'automatic code generation'. Let me take a look. If we don't need it since the 
attribute for C
only. The implementation can be just a unreachable failure or somehow I will 
see whether I can
tweak the auto code generation to avoid instantiating this function. 


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120296/new/

https://reviews.llvm.org/D120296

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D121715: [Clang] Fix an unused-but-set-variable warning with compond assignment

2022-03-15 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

@eli.friedman Please feel free adding other reviewers if needed. Thanks!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D121715/new/

https://reviews.llvm.org/D121715

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D121715: [Clang] Fix an unused-but-set-variable warning with compond assignment

2022-03-15 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song created this revision.
yonghong-song added a reviewer: eli.friedman.
Herald added a project: All.
yonghong-song requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

For the following code,

  void test() {
  int j = 0;
  for (int i = 0; i < 1000; i++)
  j += 1;
  return;
  } 

If compiled with

  clang -g -Wall -Werror -S -emit-llvm test.c

we will see the following error:

  test.c:2:6: error: variable 'j' set but not used 
[-Werror,-Wunused-but-set-variable]
  int j = 0;
  ^   

This is not quite right since 'j' is indeed used due to '+=' operator.
gcc doesn't emit error either in this case.
Also if we change 'j += 1' to 'j++', the warning will disappear too 
with latest clang.

Our original use case is to use 'volatile int j = 0' with a loop like
in the above to burn cpu cycles so we could test certain kernel features.
The compilatin error will show up regardless of whether 'volatile' key 
word is used or not.

To fix the issue, in function MaybeDecrementCount(), if the 
operator is a compund assignment (i.e., +=, -=, etc.), we should
not decrement count for RefsMinusAssignments since here we have
both a reference and an assignment.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D121715

Files:
  clang/lib/Sema/SemaExprCXX.cpp
  clang/test/Sema/warn-unused-but-set-variables.c


Index: clang/test/Sema/warn-unused-but-set-variables.c
===
--- clang/test/Sema/warn-unused-but-set-variables.c
+++ clang/test/Sema/warn-unused-but-set-variables.c
@@ -59,3 +59,10 @@
   __attribute__((__cleanup__(for_cleanup))) int x;
   x = 5;
 }
+
+void f4() {
+  int j = 0;
+  for (int i = 0; i < 1000; i++)
+j += 1;
+  return;
+}
Index: clang/lib/Sema/SemaExprCXX.cpp
===
--- clang/lib/Sema/SemaExprCXX.cpp
+++ clang/lib/Sema/SemaExprCXX.cpp
@@ -7927,7 +7927,7 @@
 BO->getRHS()->getType()->isDependentType()) {
   if (BO->getOpcode() != BO_Assign)
 return;
-} else if (!BO->isAssignmentOp())
+} else if (!BO->isAssignmentOp() || BO->isCompoundAssignmentOp())
   return;
 LHS = dyn_cast(BO->getLHS());
   } else if (CXXOperatorCallExpr *COCE = dyn_cast(E)) {


Index: clang/test/Sema/warn-unused-but-set-variables.c
===
--- clang/test/Sema/warn-unused-but-set-variables.c
+++ clang/test/Sema/warn-unused-but-set-variables.c
@@ -59,3 +59,10 @@
   __attribute__((__cleanup__(for_cleanup))) int x;
   x = 5;
 }
+
+void f4() {
+  int j = 0;
+  for (int i = 0; i < 1000; i++)
+j += 1;
+  return;
+}
Index: clang/lib/Sema/SemaExprCXX.cpp
===
--- clang/lib/Sema/SemaExprCXX.cpp
+++ clang/lib/Sema/SemaExprCXX.cpp
@@ -7927,7 +7927,7 @@
 BO->getRHS()->getType()->isDependentType()) {
   if (BO->getOpcode() != BO_Assign)
 return;
-} else if (!BO->isAssignmentOp())
+} else if (!BO->isAssignmentOp() || BO->isCompoundAssignmentOp())
   return;
 LHS = dyn_cast(BO->getLHS());
   } else if (CXXOperatorCallExpr *COCE = dyn_cast(E)) {
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120296: [Attr] Fix a btf_type_tag AST generation bug

2022-03-15 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 415326.
yonghong-song added a comment.

- add one more _Generic test.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120296/new/

https://reviews.llvm.org/D120296

Files:
  clang/include/clang-c/Index.h
  clang/include/clang/AST/ASTContext.h
  clang/include/clang/AST/ASTNodeTraverser.h
  clang/include/clang/AST/PropertiesBase.td
  clang/include/clang/AST/RecursiveASTVisitor.h
  clang/include/clang/AST/Type.h
  clang/include/clang/AST/TypeLoc.h
  clang/include/clang/AST/TypeProperties.td
  clang/include/clang/Basic/TypeNodes.td
  clang/include/clang/Serialization/ASTRecordReader.h
  clang/include/clang/Serialization/ASTRecordWriter.h
  clang/include/clang/Serialization/TypeBitCodes.def
  clang/lib/AST/ASTContext.cpp
  clang/lib/AST/ASTStructuralEquivalence.cpp
  clang/lib/AST/ItaniumMangle.cpp
  clang/lib/AST/TypeLoc.cpp
  clang/lib/AST/TypePrinter.cpp
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/CodeGen/CGDebugInfo.h
  clang/lib/CodeGen/CodeGenFunction.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaType.cpp
  clang/lib/Sema/TreeTransform.h
  clang/lib/Serialization/ASTReader.cpp
  clang/lib/Serialization/ASTWriter.cpp
  clang/test/CodeGen/attr-btf_type_tag-similar-type.c
  clang/test/PCH/btf_type_tag_attr.c
  clang/test/Sema/attr-btf_type_tag.c
  clang/tools/libclang/CIndex.cpp
  clang/tools/libclang/CXType.cpp

Index: clang/tools/libclang/CXType.cpp
===
--- clang/tools/libclang/CXType.cpp
+++ clang/tools/libclang/CXType.cpp
@@ -116,6 +116,7 @@
 TKCASE(Elaborated);
 TKCASE(Pipe);
 TKCASE(Attributed);
+TKCASE(BTFTagAttributed);
 TKCASE(Atomic);
 default:
   return CXType_Unexposed;
@@ -136,6 +137,10 @@
 return MakeCXType(ATT->getEquivalentType(), TU);
   }
 }
+if (auto *ATT = T->getAs()) {
+  if (!(TU->ParsingOptions & CXTranslationUnit_IncludeAttributedTypes))
+return MakeCXType(ATT->getWrappedType(), TU);
+}
 // Handle paren types as the original type
 if (auto *PTT = T->getAs()) {
   return MakeCXType(PTT->getInnerType(), TU);
@@ -610,6 +615,7 @@
 TKIND(Elaborated);
 TKIND(Pipe);
 TKIND(Attributed);
+TKIND(BTFTagAttributed);
 TKIND(BFloat16);
 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) TKIND(Id);
 #include "clang/Basic/OpenCLImageTypes.def"
@@ -1051,6 +1057,9 @@
   if (auto *ATT = T->getAs())
 return MakeCXType(ATT->getModifiedType(), GetTU(CT));
 
+  if (auto *ATT = T->getAs())
+return MakeCXType(ATT->getWrappedType(), GetTU(CT));
+
   return MakeCXType(QualType(), GetTU(CT));
 }
 
Index: clang/tools/libclang/CIndex.cpp
===
--- clang/tools/libclang/CIndex.cpp
+++ clang/tools/libclang/CIndex.cpp
@@ -1672,6 +1672,10 @@
   return Visit(TL.getModifiedLoc());
 }
 
+bool CursorVisitor::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
+  return Visit(TL.getWrappedLoc());
+}
+
 bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL,
  bool SkipResultType) {
   if (!SkipResultType && Visit(TL.getReturnLoc()))
Index: clang/test/Sema/attr-btf_type_tag.c
===
--- clang/test/Sema/attr-btf_type_tag.c
+++ clang/test/Sema/attr-btf_type_tag.c
@@ -23,3 +23,19 @@
 int __tag4 * __tag5 * __tag6 *foo1(struct t __tag1 * __tag2 * __tag3 *a1) {
   return (int __tag4 * __tag5 * __tag6 *)a1[0][0]->d;
 }
+
+// The btf_type_tag attribute will be ignored during _Generic type matching
+int g1 = _Generic((int *)0, int __tag1 *: 0);
+int g2 = _Generic((int __tag1 *)0, int *: 0);
+int g3 = _Generic(0,
+  int __tag1 * : 0, // expected-note {{compatible type 'int  btf_type_tag(tag1)*' (aka 'int *') specified here}}
+  int * : 0, // expected-error {{type 'int *' in generic association compatible with previously specified type 'int  btf_type_tag(tag1)*' (aka 'int *')}}
+  default : 0);
+
+// The btf_type_tag attribute will be ignored during overloadable type matching
+void bar2(int __tag1 *a) __attribute__((overloadable)) { asm volatile (""); } // expected-note {{previous definition is here}}
+void bar2(int *a) __attribute__((overloadable)) { asm volatile (""); } // expected-error {{redefinition of 'bar2'}}
+void foo2(int __tag1 *a, int *b) {
+  bar2(a);
+  bar2(b);
+}
Index: clang/test/PCH/btf_type_tag_attr.c
===
--- /dev/null
+++ clang/test/PCH/btf_type_tag_attr.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -emit-pch -o %t %s
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -include-pch %t \
+// RUN:   -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+int __attribute__((btf_type_tag("tag1"))) __attribute__((btf_type_tag("tag2"))) *p;
Index: 

[PATCH] D120296: [Attr] Fix a btf_type_tag AST generation bug

2022-03-15 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added inline comments.



Comment at: clang/include/clang/AST/TypeLoc.h:923
+  void initializeLocal(ASTContext , SourceLocation loc) {}
+
+  QualType getInnerType() const { return getTypePtr()->getWrappedType(); }

yonghong-song wrote:
> aaron.ballman wrote:
> > yonghong-song wrote:
> > > aaron.ballman wrote:
> > > > Do we also need something like this? 
> > > > https://github.com/llvm/llvm-project/blob/main/clang/include/clang/AST/TypeLoc.h#L1187
> > > I didn't see getLocalDataSize() in AttributedTypeLoc so it is not needed 
> > > for AttributedTypeLoc. BTFTagAttributedTypeLoc usage is very similar to 
> > > AttributedTypeLoc, so I think we are fine here.
> > The main difference is that `AttributedLocInfo` has a member and 
> > `BTFTagAttributedLocInfo` is empty. This is why I think it's closer to an 
> > `AdjustedLocInfo` object which also is an empty struct.
> I think adding getLocalDataSize() return 0 should be OK. But it looks like we 
> don't need to do it, at least for BTFTagAttributedTypeLoc. The following are 
> some details.
> 
> The parent class of BTFTagAttributedTypeLoc is ConcreteTypeLoc, which 
> contains:
> 
>   unsigned getLocalDataSize() const {
> unsigned size = sizeof(LocalData);
> unsigned extraAlign = asDerived()->getExtraLocalDataAlignment();
> size = llvm::alignTo(size, extraAlign);
> size += asDerived()->getExtraLocalDataSize();
> return size;
>   }
> 
>   unsigned getExtraLocalDataSize() const {
> return 0;
>   }
> 
>   unsigned getExtraLocalDataAlignment() const {
> return 1;
>   } 
> 
> Manually calculating getLocalDataSize() can get the same return value 0. So I 
> think it is okay not to define  getLocalDataSize in BTFTagAttributedTypeLoc. 
> 
> The AdjustedLocInfo seems having some inconsistency, at least based on 
> comments:
> 
> struct AdjustedLocInfo {}; // Nothing.
>   unsigned getLocalDataSize() const {
> // sizeof(AdjustedLocInfo) is 1, but we don't need its address to be 
> unique
> // anyway.  TypeLocBuilder can't handle data sizes of 1.
> return 0;  // No data.
>   }
> Maybe previously AdjustedLocInfo size is 1 and the implementation of 
> getLocalDataSize() to set size to 0 explicitly in which case, the function is 
> needed inside AdjustedTypeLoc. It might be needed now since AdjustedLocInfo 
> size is 0.
> 
For
> Maybe previously AdjustedLocInfo size is 1 and the implementation of 
> getLocalDataSize() to set size to 0 explicitly in which case, the function is 
> needed inside AdjustedTypeLoc. It might be needed now since AdjustedLocInfo 
> size is 0.
I mean "it might not be needed now since AdjustedLocInfo size is 0".


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120296/new/

https://reviews.llvm.org/D120296

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120296: [Attr] Fix a btf_type_tag AST generation bug

2022-03-15 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added inline comments.



Comment at: clang/include/clang/AST/TypeLoc.h:923
+  void initializeLocal(ASTContext , SourceLocation loc) {}
+
+  QualType getInnerType() const { return getTypePtr()->getWrappedType(); }

aaron.ballman wrote:
> yonghong-song wrote:
> > aaron.ballman wrote:
> > > Do we also need something like this? 
> > > https://github.com/llvm/llvm-project/blob/main/clang/include/clang/AST/TypeLoc.h#L1187
> > I didn't see getLocalDataSize() in AttributedTypeLoc so it is not needed 
> > for AttributedTypeLoc. BTFTagAttributedTypeLoc usage is very similar to 
> > AttributedTypeLoc, so I think we are fine here.
> The main difference is that `AttributedLocInfo` has a member and 
> `BTFTagAttributedLocInfo` is empty. This is why I think it's closer to an 
> `AdjustedLocInfo` object which also is an empty struct.
I think adding getLocalDataSize() return 0 should be OK. But it looks like we 
don't need to do it, at least for BTFTagAttributedTypeLoc. The following are 
some details.

The parent class of BTFTagAttributedTypeLoc is ConcreteTypeLoc, which contains:

  unsigned getLocalDataSize() const {
unsigned size = sizeof(LocalData);
unsigned extraAlign = asDerived()->getExtraLocalDataAlignment();
size = llvm::alignTo(size, extraAlign);
size += asDerived()->getExtraLocalDataSize();
return size;
  }

  unsigned getExtraLocalDataSize() const {
return 0;
  }

  unsigned getExtraLocalDataAlignment() const {
return 1;
  } 

Manually calculating getLocalDataSize() can get the same return value 0. So I 
think it is okay not to define  getLocalDataSize in BTFTagAttributedTypeLoc. 

The AdjustedLocInfo seems having some inconsistency, at least based on comments:

struct AdjustedLocInfo {}; // Nothing.
  unsigned getLocalDataSize() const {
// sizeof(AdjustedLocInfo) is 1, but we don't need its address to be unique
// anyway.  TypeLocBuilder can't handle data sizes of 1.
return 0;  // No data.
  }
Maybe previously AdjustedLocInfo size is 1 and the implementation of 
getLocalDataSize() to set size to 0 explicitly in which case, the function is 
needed inside AdjustedTypeLoc. It might be needed now since AdjustedLocInfo 
size is 0.




Comment at: clang/test/Sema/attr-btf_type_tag.c:29
+int g1 = _Generic((int *)0, int __tag1 *: 0);
+int g2 = _Generic((int __tag1 *)0, int *: 0);
+

aaron.ballman wrote:
> I'd like a third test case:
> ```
> int g3 = _Generic(0, int __tag1 * : 0, int * : 0, default : 0); // 
> expected-error {{something about duplicate compatible associations}}
> ```
Will add. I will break the above test into multiple lines. Otherwise, there 
will be multiple errors in the same line.



Comment at: clang/test/Sema/attr-btf_type_tag.c:31-37
+// The btf_type_tag attribute will be ignored during overloadable type matching
+void __attribute__((overloadable)) bar1(int __tag1 *a);
+void __attribute__((overloadable)) bar2(int *a);
+void foo2(int *a, int __tag1 *b) {
+  bar1(a);
+  bar2(b);
+}

aaron.ballman wrote:
> yonghong-song wrote:
> > aaron.ballman wrote:
> > > This test looks confused -- those functions have different names, so 
> > > they're not overloading one another. Even if they were named with the 
> > > same identifier, it wouldn't be sufficient to test which gets called (you 
> > > can redeclare overloads as in https://godbolt.org/z/aebMbY1aM).
> > > 
> > > I think this case needs a codegen test to see which function is actually 
> > > dispatched to.
> > sorry for confusion. the test is bad. For the example in the above 
> > godbolt.org link, we have
> > ```
> > __attribute__((overloadable)) void func(int a);
> > __attribute__((overloadable)) void func(int a);
> > ```
> > Such multiple declarations are actually okay since we allow duplicated 
> > declarations. As you mentioned, we really want to know which function 
> > definition it picks with overloadable attribute.
> > I tried a few examples:
> > ```
> > $ cat test.c
> > #if 1
> > #define __attr __attribute__((btf_type_tag("tag1")))
> > #else
> > #define __attr __attribute__((noderef))
> > #endif
> > 
> > void bar1(int __attr *);
> > void bar2(int *);
> > void isdigit1(int __attr *a) __attribute__((overloadable)) {
> >   bar1(a);
> > }
> > void isdigit1(int *a) __attribute__((overloadable)) {
> >   bar2(a);
> > }
> > 
> > void foo(int __attr *a, int *b) {
> >   isdigit1(a);
> >   isdigit1(b);
> > }
> > 
> > $ clang -g -std=c2x test.c -S -O2 -emit-llvm -DUSE_TAG
> > test.c:12:6: error: redefinition of 'isdigit1'
> > void isdigit1(int *a) __attribute__((overloadable)) {
> >  ^
> > test.c:9:6: note: previous definition is here
> > void isdigit1(int __attr *a) __attribute__((overloadable)) {
> >  ^
> > 1 error generated.
> > $ clang -g -std=c2x test.c -S -O2 -emit-llvm
> > test.c:12:6: error: redefinition of 'isdigit1'
> > void isdigit1(int *a) __attribute__((overloadable)) {
> >  ^

[PATCH] D120296: [Attr] Fix a btf_type_tag AST generation bug

2022-03-12 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 414872.
yonghong-song added a comment.

- fix one 'auto' declaration issue
- rework on the test with overloadable attribute.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120296/new/

https://reviews.llvm.org/D120296

Files:
  clang/include/clang-c/Index.h
  clang/include/clang/AST/ASTContext.h
  clang/include/clang/AST/ASTNodeTraverser.h
  clang/include/clang/AST/PropertiesBase.td
  clang/include/clang/AST/RecursiveASTVisitor.h
  clang/include/clang/AST/Type.h
  clang/include/clang/AST/TypeLoc.h
  clang/include/clang/AST/TypeProperties.td
  clang/include/clang/Basic/TypeNodes.td
  clang/include/clang/Serialization/ASTRecordReader.h
  clang/include/clang/Serialization/ASTRecordWriter.h
  clang/include/clang/Serialization/TypeBitCodes.def
  clang/lib/AST/ASTContext.cpp
  clang/lib/AST/ASTStructuralEquivalence.cpp
  clang/lib/AST/ItaniumMangle.cpp
  clang/lib/AST/TypeLoc.cpp
  clang/lib/AST/TypePrinter.cpp
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/CodeGen/CGDebugInfo.h
  clang/lib/CodeGen/CodeGenFunction.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaType.cpp
  clang/lib/Sema/TreeTransform.h
  clang/lib/Serialization/ASTReader.cpp
  clang/lib/Serialization/ASTWriter.cpp
  clang/test/CodeGen/attr-btf_type_tag-similar-type.c
  clang/test/PCH/btf_type_tag_attr.c
  clang/test/Sema/attr-btf_type_tag.c
  clang/tools/libclang/CIndex.cpp
  clang/tools/libclang/CXType.cpp

Index: clang/tools/libclang/CXType.cpp
===
--- clang/tools/libclang/CXType.cpp
+++ clang/tools/libclang/CXType.cpp
@@ -116,6 +116,7 @@
 TKCASE(Elaborated);
 TKCASE(Pipe);
 TKCASE(Attributed);
+TKCASE(BTFTagAttributed);
 TKCASE(Atomic);
 default:
   return CXType_Unexposed;
@@ -136,6 +137,10 @@
 return MakeCXType(ATT->getEquivalentType(), TU);
   }
 }
+if (auto *ATT = T->getAs()) {
+  if (!(TU->ParsingOptions & CXTranslationUnit_IncludeAttributedTypes))
+return MakeCXType(ATT->getWrappedType(), TU);
+}
 // Handle paren types as the original type
 if (auto *PTT = T->getAs()) {
   return MakeCXType(PTT->getInnerType(), TU);
@@ -610,6 +615,7 @@
 TKIND(Elaborated);
 TKIND(Pipe);
 TKIND(Attributed);
+TKIND(BTFTagAttributed);
 TKIND(BFloat16);
 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) TKIND(Id);
 #include "clang/Basic/OpenCLImageTypes.def"
@@ -1051,6 +1057,9 @@
   if (auto *ATT = T->getAs())
 return MakeCXType(ATT->getModifiedType(), GetTU(CT));
 
+  if (auto *ATT = T->getAs())
+return MakeCXType(ATT->getWrappedType(), GetTU(CT));
+
   return MakeCXType(QualType(), GetTU(CT));
 }
 
Index: clang/tools/libclang/CIndex.cpp
===
--- clang/tools/libclang/CIndex.cpp
+++ clang/tools/libclang/CIndex.cpp
@@ -1672,6 +1672,10 @@
   return Visit(TL.getModifiedLoc());
 }
 
+bool CursorVisitor::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
+  return Visit(TL.getWrappedLoc());
+}
+
 bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL,
  bool SkipResultType) {
   if (!SkipResultType && Visit(TL.getReturnLoc()))
Index: clang/test/Sema/attr-btf_type_tag.c
===
--- clang/test/Sema/attr-btf_type_tag.c
+++ clang/test/Sema/attr-btf_type_tag.c
@@ -23,3 +23,15 @@
 int __tag4 * __tag5 * __tag6 *foo1(struct t __tag1 * __tag2 * __tag3 *a1) {
   return (int __tag4 * __tag5 * __tag6 *)a1[0][0]->d;
 }
+
+// The btf_type_tag attribute will be ignored during _Generic type matching
+int g1 = _Generic((int *)0, int __tag1 *: 0);
+int g2 = _Generic((int __tag1 *)0, int *: 0);
+
+// The btf_type_tag attribute will be ignored during overloadable type matching
+void bar2(int __tag1 *a) __attribute__((overloadable)) { asm volatile (""); } // expected-note {{previous definition is here}}
+void bar2(int *a) __attribute__((overloadable)) { asm volatile (""); } // expected-error {{redefinition of 'bar2'}}
+void foo2(int __tag1 *a, int *b) {
+  bar2(a);
+  bar2(b);
+}
Index: clang/test/PCH/btf_type_tag_attr.c
===
--- /dev/null
+++ clang/test/PCH/btf_type_tag_attr.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -emit-pch -o %t %s
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -include-pch %t \
+// RUN:   -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+int __attribute__((btf_type_tag("tag1"))) __attribute__((btf_type_tag("tag2"))) *p;
Index: clang/test/CodeGen/attr-btf_type_tag-similar-type.c
===
--- /dev/null
+++ clang/test/CodeGen/attr-btf_type_tag-similar-type.c
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -debug-info-kind=limited -S -emit-llvm -o - %s | FileCheck %s
+
+struct map_value {
+   

[PATCH] D120296: [Attr] Fix a btf_type_tag AST generation bug

2022-03-11 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added inline comments.



Comment at: clang/include/clang/AST/TypeLoc.h:923
+  void initializeLocal(ASTContext , SourceLocation loc) {}
+
+  QualType getInnerType() const { return getTypePtr()->getWrappedType(); }

aaron.ballman wrote:
> Do we also need something like this? 
> https://github.com/llvm/llvm-project/blob/main/clang/include/clang/AST/TypeLoc.h#L1187
I didn't see getLocalDataSize() in AttributedTypeLoc so it is not needed for 
AttributedTypeLoc. BTFTagAttributedTypeLoc usage is very similar to 
AttributedTypeLoc, so I think we are fine here.



Comment at: clang/lib/CodeGen/CGDebugInfo.cpp:1144
+  SmallVector Annots;
+  auto BTFTagAttrTy = dyn_cast(PointeeTy);
+  while (BTFTagAttrTy) {

aaron.ballman wrote:
> aaron.ballman wrote:
> > 
> This one may have been missed.
You mean:
```
auto *BTFTagAttrTy = dyn_cast(PointeeTy);
```
I missed it. Will fix in the next revision.



Comment at: clang/test/Sema/attr-btf_type_tag.c:31-37
+// The btf_type_tag attribute will be ignored during overloadable type matching
+void __attribute__((overloadable)) bar1(int __tag1 *a);
+void __attribute__((overloadable)) bar2(int *a);
+void foo2(int *a, int __tag1 *b) {
+  bar1(a);
+  bar2(b);
+}

aaron.ballman wrote:
> This test looks confused -- those functions have different names, so they're 
> not overloading one another. Even if they were named with the same 
> identifier, it wouldn't be sufficient to test which gets called (you can 
> redeclare overloads as in https://godbolt.org/z/aebMbY1aM).
> 
> I think this case needs a codegen test to see which function is actually 
> dispatched to.
sorry for confusion. the test is bad. For the example in the above godbolt.org 
link, we have
```
__attribute__((overloadable)) void func(int a);
__attribute__((overloadable)) void func(int a);
```
Such multiple declarations are actually okay since we allow duplicated 
declarations. As you mentioned, we really want to know which function 
definition it picks with overloadable attribute.
I tried a few examples:
```
$ cat test.c
#if 1
#define __attr __attribute__((btf_type_tag("tag1")))
#else
#define __attr __attribute__((noderef))
#endif

void bar1(int __attr *);
void bar2(int *);
void isdigit1(int __attr *a) __attribute__((overloadable)) {
  bar1(a);
}
void isdigit1(int *a) __attribute__((overloadable)) {
  bar2(a);
}

void foo(int __attr *a, int *b) {
  isdigit1(a);
  isdigit1(b);
}

$ clang -g -std=c2x test.c -S -O2 -emit-llvm -DUSE_TAG
test.c:12:6: error: redefinition of 'isdigit1'
void isdigit1(int *a) __attribute__((overloadable)) {
 ^
test.c:9:6: note: previous definition is here
void isdigit1(int __attr *a) __attribute__((overloadable)) {
 ^
1 error generated.
$ clang -g -std=c2x test.c -S -O2 -emit-llvm
test.c:12:6: error: redefinition of 'isdigit1'
void isdigit1(int *a) __attribute__((overloadable)) {
 ^
test.c:9:6: note: previous definition is here
void isdigit1(int __attr *a) __attribute__((overloadable)) {
 ^
1 error generated.
$
```

so just attribute itself cannot differentiate overloadable functions. In the 
above test.c, if I changed one of isdigle1 and corresponding bar?() function 
from 'int' to 'float', compilation can succeed.

So I will add overloadable failure and success case in Sema/ directory. I am 
not sure whether a codegen case is needed for the case like
```
void bar1(float __attr *);
void bar2(int *);
void isdigit1(float __attr *a) __attribute__((overloadable)) {
  bar1(a);
}
void isdigit1(int *a) __attribute__((overloadable)) {
  bar2(a);
}
```
or not. But if you think it is necessary, I am happy to add it.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120296/new/

https://reviews.llvm.org/D120296

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120296: [Attr] Fix a btf_type_tag AST generation bug

2022-03-09 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

@aaron.ballman @erichkeane Hopefully I addressed your comments. Could you take 
a look again?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120296/new/

https://reviews.llvm.org/D120296

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120296: [Attr] Fix a btf_type_tag AST generation bug

2022-03-09 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 414267.
yonghong-song added a comment.

- use BTFTypeTagAttr instead of Attr in PropertiesBase.td


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120296/new/

https://reviews.llvm.org/D120296

Files:
  clang/include/clang-c/Index.h
  clang/include/clang/AST/ASTContext.h
  clang/include/clang/AST/ASTNodeTraverser.h
  clang/include/clang/AST/PropertiesBase.td
  clang/include/clang/AST/RecursiveASTVisitor.h
  clang/include/clang/AST/Type.h
  clang/include/clang/AST/TypeLoc.h
  clang/include/clang/AST/TypeProperties.td
  clang/include/clang/Basic/TypeNodes.td
  clang/include/clang/Serialization/ASTRecordReader.h
  clang/include/clang/Serialization/ASTRecordWriter.h
  clang/include/clang/Serialization/TypeBitCodes.def
  clang/lib/AST/ASTContext.cpp
  clang/lib/AST/ASTStructuralEquivalence.cpp
  clang/lib/AST/ItaniumMangle.cpp
  clang/lib/AST/TypeLoc.cpp
  clang/lib/AST/TypePrinter.cpp
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/CodeGen/CGDebugInfo.h
  clang/lib/CodeGen/CodeGenFunction.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaType.cpp
  clang/lib/Sema/TreeTransform.h
  clang/lib/Serialization/ASTReader.cpp
  clang/lib/Serialization/ASTWriter.cpp
  clang/test/CodeGen/attr-btf_type_tag-similar-type.c
  clang/test/PCH/btf_type_tag_attr.c
  clang/test/Sema/attr-btf_type_tag.c
  clang/tools/libclang/CIndex.cpp
  clang/tools/libclang/CXType.cpp

Index: clang/tools/libclang/CXType.cpp
===
--- clang/tools/libclang/CXType.cpp
+++ clang/tools/libclang/CXType.cpp
@@ -116,6 +116,7 @@
 TKCASE(Elaborated);
 TKCASE(Pipe);
 TKCASE(Attributed);
+TKCASE(BTFTagAttributed);
 TKCASE(Atomic);
 default:
   return CXType_Unexposed;
@@ -136,6 +137,10 @@
 return MakeCXType(ATT->getEquivalentType(), TU);
   }
 }
+if (auto *ATT = T->getAs()) {
+  if (!(TU->ParsingOptions & CXTranslationUnit_IncludeAttributedTypes))
+return MakeCXType(ATT->getWrappedType(), TU);
+}
 // Handle paren types as the original type
 if (auto *PTT = T->getAs()) {
   return MakeCXType(PTT->getInnerType(), TU);
@@ -610,6 +615,7 @@
 TKIND(Elaborated);
 TKIND(Pipe);
 TKIND(Attributed);
+TKIND(BTFTagAttributed);
 TKIND(BFloat16);
 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) TKIND(Id);
 #include "clang/Basic/OpenCLImageTypes.def"
@@ -1051,6 +1057,9 @@
   if (auto *ATT = T->getAs())
 return MakeCXType(ATT->getModifiedType(), GetTU(CT));
 
+  if (auto *ATT = T->getAs())
+return MakeCXType(ATT->getWrappedType(), GetTU(CT));
+
   return MakeCXType(QualType(), GetTU(CT));
 }
 
Index: clang/tools/libclang/CIndex.cpp
===
--- clang/tools/libclang/CIndex.cpp
+++ clang/tools/libclang/CIndex.cpp
@@ -1672,6 +1672,10 @@
   return Visit(TL.getModifiedLoc());
 }
 
+bool CursorVisitor::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
+  return Visit(TL.getWrappedLoc());
+}
+
 bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL,
  bool SkipResultType) {
   if (!SkipResultType && Visit(TL.getReturnLoc()))
Index: clang/test/Sema/attr-btf_type_tag.c
===
--- clang/test/Sema/attr-btf_type_tag.c
+++ clang/test/Sema/attr-btf_type_tag.c
@@ -23,3 +23,15 @@
 int __tag4 * __tag5 * __tag6 *foo1(struct t __tag1 * __tag2 * __tag3 *a1) {
   return (int __tag4 * __tag5 * __tag6 *)a1[0][0]->d;
 }
+
+// The btf_type_tag attribute will be ignored during _Generic type matching
+int g1 = _Generic((int *)0, int __tag1 *: 0);
+int g2 = _Generic((int __tag1 *)0, int *: 0);
+
+// The btf_type_tag attribute will be ignored during overloadable type matching
+void __attribute__((overloadable)) bar1(int __tag1 *a);
+void __attribute__((overloadable)) bar2(int *a);
+void foo2(int *a, int __tag1 *b) {
+  bar1(a);
+  bar2(b);
+}
Index: clang/test/PCH/btf_type_tag_attr.c
===
--- /dev/null
+++ clang/test/PCH/btf_type_tag_attr.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -emit-pch -o %t %s
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -include-pch %t \
+// RUN:   -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+int __attribute__((btf_type_tag("tag1"))) __attribute__((btf_type_tag("tag2"))) *p;
Index: clang/test/CodeGen/attr-btf_type_tag-similar-type.c
===
--- /dev/null
+++ clang/test/CodeGen/attr-btf_type_tag-similar-type.c
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -debug-info-kind=limited -S -emit-llvm -o - %s | FileCheck %s
+
+struct map_value {
+int __attribute__((btf_type_tag("tag1"))) __attribute__((btf_type_tag("tag3"))) *a;
+int 

[PATCH] D120296: [Attr] Fix a btf_type_tag AST generation bug

2022-03-09 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 414263.
yonghong-song added a comment.

- further clang-format fixes.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120296/new/

https://reviews.llvm.org/D120296

Files:
  clang/include/clang-c/Index.h
  clang/include/clang/AST/ASTContext.h
  clang/include/clang/AST/ASTNodeTraverser.h
  clang/include/clang/AST/PropertiesBase.td
  clang/include/clang/AST/RecursiveASTVisitor.h
  clang/include/clang/AST/Type.h
  clang/include/clang/AST/TypeLoc.h
  clang/include/clang/AST/TypeProperties.td
  clang/include/clang/Basic/TypeNodes.td
  clang/include/clang/Serialization/ASTRecordWriter.h
  clang/include/clang/Serialization/TypeBitCodes.def
  clang/lib/AST/ASTContext.cpp
  clang/lib/AST/ASTStructuralEquivalence.cpp
  clang/lib/AST/ItaniumMangle.cpp
  clang/lib/AST/TypeLoc.cpp
  clang/lib/AST/TypePrinter.cpp
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/CodeGen/CGDebugInfo.h
  clang/lib/CodeGen/CodeGenFunction.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaType.cpp
  clang/lib/Sema/TreeTransform.h
  clang/lib/Serialization/ASTReader.cpp
  clang/lib/Serialization/ASTWriter.cpp
  clang/test/CodeGen/attr-btf_type_tag-similar-type.c
  clang/test/PCH/btf_type_tag_attr.c
  clang/test/Sema/attr-btf_type_tag.c
  clang/tools/libclang/CIndex.cpp
  clang/tools/libclang/CXType.cpp

Index: clang/tools/libclang/CXType.cpp
===
--- clang/tools/libclang/CXType.cpp
+++ clang/tools/libclang/CXType.cpp
@@ -116,6 +116,7 @@
 TKCASE(Elaborated);
 TKCASE(Pipe);
 TKCASE(Attributed);
+TKCASE(BTFTagAttributed);
 TKCASE(Atomic);
 default:
   return CXType_Unexposed;
@@ -136,6 +137,10 @@
 return MakeCXType(ATT->getEquivalentType(), TU);
   }
 }
+if (auto *ATT = T->getAs()) {
+  if (!(TU->ParsingOptions & CXTranslationUnit_IncludeAttributedTypes))
+return MakeCXType(ATT->getWrappedType(), TU);
+}
 // Handle paren types as the original type
 if (auto *PTT = T->getAs()) {
   return MakeCXType(PTT->getInnerType(), TU);
@@ -610,6 +615,7 @@
 TKIND(Elaborated);
 TKIND(Pipe);
 TKIND(Attributed);
+TKIND(BTFTagAttributed);
 TKIND(BFloat16);
 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) TKIND(Id);
 #include "clang/Basic/OpenCLImageTypes.def"
@@ -1051,6 +1057,9 @@
   if (auto *ATT = T->getAs())
 return MakeCXType(ATT->getModifiedType(), GetTU(CT));
 
+  if (auto *ATT = T->getAs())
+return MakeCXType(ATT->getWrappedType(), GetTU(CT));
+
   return MakeCXType(QualType(), GetTU(CT));
 }
 
Index: clang/tools/libclang/CIndex.cpp
===
--- clang/tools/libclang/CIndex.cpp
+++ clang/tools/libclang/CIndex.cpp
@@ -1672,6 +1672,10 @@
   return Visit(TL.getModifiedLoc());
 }
 
+bool CursorVisitor::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
+  return Visit(TL.getWrappedLoc());
+}
+
 bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL,
  bool SkipResultType) {
   if (!SkipResultType && Visit(TL.getReturnLoc()))
Index: clang/test/Sema/attr-btf_type_tag.c
===
--- clang/test/Sema/attr-btf_type_tag.c
+++ clang/test/Sema/attr-btf_type_tag.c
@@ -23,3 +23,15 @@
 int __tag4 * __tag5 * __tag6 *foo1(struct t __tag1 * __tag2 * __tag3 *a1) {
   return (int __tag4 * __tag5 * __tag6 *)a1[0][0]->d;
 }
+
+// The btf_type_tag attribute will be ignored during _Generic type matching
+int g1 = _Generic((int *)0, int __tag1 *: 0);
+int g2 = _Generic((int __tag1 *)0, int *: 0);
+
+// The btf_type_tag attribute will be ignored during overloadable type matching
+void __attribute__((overloadable)) bar1(int __tag1 *a);
+void __attribute__((overloadable)) bar2(int *a);
+void foo2(int *a, int __tag1 *b) {
+  bar1(a);
+  bar2(b);
+}
Index: clang/test/PCH/btf_type_tag_attr.c
===
--- /dev/null
+++ clang/test/PCH/btf_type_tag_attr.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -emit-pch -o %t %s
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -include-pch %t \
+// RUN:   -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+int __attribute__((btf_type_tag("tag1"))) __attribute__((btf_type_tag("tag2"))) *p;
Index: clang/test/CodeGen/attr-btf_type_tag-similar-type.c
===
--- /dev/null
+++ clang/test/CodeGen/attr-btf_type_tag-similar-type.c
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -debug-info-kind=limited -S -emit-llvm -o - %s | FileCheck %s
+
+struct map_value {
+int __attribute__((btf_type_tag("tag1"))) __attribute__((btf_type_tag("tag3"))) *a;
+int __attribute__((btf_type_tag("tag2"))) __attribute__((btf_type_tag("tag4"))) *b;
+};
+
+struct map_value 

[PATCH] D120296: [Attr] Fix a btf_type_tag AST generation bug

2022-03-09 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 414252.
yonghong-song added a comment.

- clang-format fix
- change variable names to be llvm compliant.
- remove Attr from BTFTagAttributedTypeLoc as it can be retrieved from the 
BTFTagAttributedType
- Also transform the attribute in TreeTransform
- add additional test with _Generic and overloadable attribute


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120296/new/

https://reviews.llvm.org/D120296

Files:
  clang/include/clang-c/Index.h
  clang/include/clang/AST/ASTContext.h
  clang/include/clang/AST/ASTNodeTraverser.h
  clang/include/clang/AST/PropertiesBase.td
  clang/include/clang/AST/RecursiveASTVisitor.h
  clang/include/clang/AST/Type.h
  clang/include/clang/AST/TypeLoc.h
  clang/include/clang/AST/TypeProperties.td
  clang/include/clang/Basic/TypeNodes.td
  clang/include/clang/Serialization/ASTRecordWriter.h
  clang/include/clang/Serialization/TypeBitCodes.def
  clang/lib/AST/ASTContext.cpp
  clang/lib/AST/ASTStructuralEquivalence.cpp
  clang/lib/AST/ItaniumMangle.cpp
  clang/lib/AST/TypeLoc.cpp
  clang/lib/AST/TypePrinter.cpp
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/CodeGen/CGDebugInfo.h
  clang/lib/CodeGen/CodeGenFunction.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaType.cpp
  clang/lib/Sema/TreeTransform.h
  clang/lib/Serialization/ASTReader.cpp
  clang/lib/Serialization/ASTWriter.cpp
  clang/test/CodeGen/attr-btf_type_tag-similar-type.c
  clang/test/PCH/btf_type_tag_attr.c
  clang/test/Sema/attr-btf_type_tag.c
  clang/tools/libclang/CIndex.cpp
  clang/tools/libclang/CXType.cpp

Index: clang/tools/libclang/CXType.cpp
===
--- clang/tools/libclang/CXType.cpp
+++ clang/tools/libclang/CXType.cpp
@@ -116,6 +116,7 @@
 TKCASE(Elaborated);
 TKCASE(Pipe);
 TKCASE(Attributed);
+TKCASE(BTFTagAttributed);
 TKCASE(Atomic);
 default:
   return CXType_Unexposed;
@@ -136,6 +137,10 @@
 return MakeCXType(ATT->getEquivalentType(), TU);
   }
 }
+if (auto *ATT = T->getAs()) {
+  if (!(TU->ParsingOptions & CXTranslationUnit_IncludeAttributedTypes))
+return MakeCXType(ATT->getWrappedType(), TU);
+}
 // Handle paren types as the original type
 if (auto *PTT = T->getAs()) {
   return MakeCXType(PTT->getInnerType(), TU);
@@ -610,6 +615,7 @@
 TKIND(Elaborated);
 TKIND(Pipe);
 TKIND(Attributed);
+TKIND(BTFTagAttributed);
 TKIND(BFloat16);
 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) TKIND(Id);
 #include "clang/Basic/OpenCLImageTypes.def"
@@ -1051,6 +1057,9 @@
   if (auto *ATT = T->getAs())
 return MakeCXType(ATT->getModifiedType(), GetTU(CT));
 
+  if (auto *ATT = T->getAs())
+return MakeCXType(ATT->getWrappedType(), GetTU(CT));
+
   return MakeCXType(QualType(), GetTU(CT));
 }
 
Index: clang/tools/libclang/CIndex.cpp
===
--- clang/tools/libclang/CIndex.cpp
+++ clang/tools/libclang/CIndex.cpp
@@ -1672,6 +1672,10 @@
   return Visit(TL.getModifiedLoc());
 }
 
+bool CursorVisitor::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
+  return Visit(TL.getWrappedLoc());
+}
+
 bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL,
  bool SkipResultType) {
   if (!SkipResultType && Visit(TL.getReturnLoc()))
Index: clang/test/Sema/attr-btf_type_tag.c
===
--- clang/test/Sema/attr-btf_type_tag.c
+++ clang/test/Sema/attr-btf_type_tag.c
@@ -23,3 +23,15 @@
 int __tag4 * __tag5 * __tag6 *foo1(struct t __tag1 * __tag2 * __tag3 *a1) {
   return (int __tag4 * __tag5 * __tag6 *)a1[0][0]->d;
 }
+
+// The btf_type_tag attribute will be ignored during _Generic type matching
+int g1 = _Generic((int *)0, int __tag1 *: 0);
+int g2 = _Generic((int __tag1 *)0, int *: 0);
+
+// The btf_type_tag attribute will be ignored during overloadable type matching
+void __attribute__((overloadable)) bar1(int __tag1 *a);
+void __attribute__((overloadable)) bar2(int *a);
+void foo2(int *a, int __tag1 *b) {
+  bar1(a);
+  bar2(b);
+}
Index: clang/test/PCH/btf_type_tag_attr.c
===
--- /dev/null
+++ clang/test/PCH/btf_type_tag_attr.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -emit-pch -o %t %s
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -include-pch %t \
+// RUN:   -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+int __attribute__((btf_type_tag("tag1"))) __attribute__((btf_type_tag("tag2"))) *p;
Index: clang/test/CodeGen/attr-btf_type_tag-similar-type.c
===
--- /dev/null
+++ clang/test/CodeGen/attr-btf_type_tag-similar-type.c
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -debug-info-kind=limited -S -emit-llvm -o - %s | 

[PATCH] D120296: [Attr] Fix a btf_type_tag AST generation bug

2022-03-09 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

@aaron.ballman Thanks for the review! I will address all your comments and will 
also add tests with `_Generic` and `__attribute__((overloadable))`.




Comment at: clang/include/clang/AST/Type.h:4797
+
+  BTFTagAttributedType(QualType canon, QualType wrapped,
+   BTFTypeTagAttr *attr)

aaron.ballman wrote:
> You should fix the formatting issue, but also, the parameter names don't 
> match the usual naming conventions (please don't replace `attr` with `Attr` 
> though; that's a type name, so go with something else like `BTFAttr`).
Will do. Sorry, I didn't spend effort on formatting itself as I know I will 
need to make revisions. But indeed should do it regardless!



Comment at: clang/include/clang/AST/Type.h:4804
+  QualType getWrappedType() const { return WrappedType; }
+  BTFTypeTagAttr *getAttr() const { return Attr; }
+

aaron.ballman wrote:
> Should this be returning a `const BTFTypeTagAttr *` instead?
Yes, will use 'const BTFTypeTagAttr *` in the next revision.



Comment at: clang/include/clang/AST/TypeLoc.h:904-917
+struct BTFTagAttributedLocInfo {
+  const Attr *TypeAttr;
+};
+
+/// Type source information for an btf_tag attributed type.
+class BTFTagAttributedTypeLoc
+: public ConcreteTypeLoc This seems a bit suspicious to me -- we are store the same attribute twice 
> (once on the `BTFTagAttributedType` and once on its type loc). Do we need to 
> do that, or can we have the type loc reach through the type pointer to get 
> the attribute from there? (Alternatively, do we want to require callers to 
> have access to an `BTFTagAttributedTypeLoc` in order to query the attribute, 
> similar to how `AttributedType`/`AttributedTypeLoc` work?)
Good point. Will change
```
struct BTFTagAttributedLocInfo {
  const Attr *TypeAttr;
};
```
to
```
struct BTFTagAttributedLocInfo {
};
```
since we can get the attribute from the `BTFTagAttributedType` itself.



Comment at: clang/include/clang/AST/TypeLoc.h:925
+
+  void initializeLocal(ASTContext , SourceLocation loc) {
+setAttr(nullptr);

aaron.ballman wrote:
> `loc` is unused? (and should have its identifier removed if this is on 
> purpose)
loc is needed in the parameter for this function. For example, in 
QualifiedTypeLoc, we have
```
  /// Initializes the local data of this type source info block to
  /// provide no information.
  void initializeLocal(ASTContext , SourceLocation Loc) {
// do nothing
  }
```



Comment at: clang/include/clang/Serialization/ASTRecordWriter.h:126-127
 
+  /// Write an Attr object.
+  void writeAttr(Attr *attr) { AddAttr(attr); }
+

aaron.ballman wrote:
> This doesn't appear to be used? Or is this called automagically because of 
> the changes in `TypeProperties.td`?
It is used because the changes in `TypeProperties.td`.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120296/new/

https://reviews.llvm.org/D120296

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120296: [Attr] Fix a btf_type_tag AST generation bug

2022-03-08 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 413995.
yonghong-song added a comment.

- add a pch test to test serialization with btf_type_tag.
- simplify the code


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120296/new/

https://reviews.llvm.org/D120296

Files:
  clang/include/clang-c/Index.h
  clang/include/clang/AST/ASTContext.h
  clang/include/clang/AST/ASTNodeTraverser.h
  clang/include/clang/AST/PropertiesBase.td
  clang/include/clang/AST/RecursiveASTVisitor.h
  clang/include/clang/AST/Type.h
  clang/include/clang/AST/TypeLoc.h
  clang/include/clang/AST/TypeProperties.td
  clang/include/clang/Basic/TypeNodes.td
  clang/include/clang/Serialization/ASTRecordWriter.h
  clang/include/clang/Serialization/TypeBitCodes.def
  clang/lib/AST/ASTContext.cpp
  clang/lib/AST/ASTStructuralEquivalence.cpp
  clang/lib/AST/ItaniumMangle.cpp
  clang/lib/AST/TypeLoc.cpp
  clang/lib/AST/TypePrinter.cpp
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/CodeGen/CGDebugInfo.h
  clang/lib/CodeGen/CodeGenFunction.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaType.cpp
  clang/lib/Sema/TreeTransform.h
  clang/lib/Serialization/ASTReader.cpp
  clang/lib/Serialization/ASTWriter.cpp
  clang/test/CodeGen/attr-btf_type_tag-similar-type.c
  clang/test/PCH/btf_type_tag_attr.c
  clang/tools/libclang/CIndex.cpp
  clang/tools/libclang/CXType.cpp

Index: clang/tools/libclang/CXType.cpp
===
--- clang/tools/libclang/CXType.cpp
+++ clang/tools/libclang/CXType.cpp
@@ -116,6 +116,7 @@
 TKCASE(Elaborated);
 TKCASE(Pipe);
 TKCASE(Attributed);
+TKCASE(BTFTagAttributed);
 TKCASE(Atomic);
 default:
   return CXType_Unexposed;
@@ -136,6 +137,10 @@
 return MakeCXType(ATT->getEquivalentType(), TU);
   }
 }
+if (auto *ATT = T->getAs()) {
+  if (!(TU->ParsingOptions & CXTranslationUnit_IncludeAttributedTypes))
+return MakeCXType(ATT->getWrappedType(), TU);
+}
 // Handle paren types as the original type
 if (auto *PTT = T->getAs()) {
   return MakeCXType(PTT->getInnerType(), TU);
@@ -610,6 +615,7 @@
 TKIND(Elaborated);
 TKIND(Pipe);
 TKIND(Attributed);
+TKIND(BTFTagAttributed);
 TKIND(BFloat16);
 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) TKIND(Id);
 #include "clang/Basic/OpenCLImageTypes.def"
@@ -1051,6 +1057,9 @@
   if (auto *ATT = T->getAs())
 return MakeCXType(ATT->getModifiedType(), GetTU(CT));
 
+  if (auto *ATT = T->getAs())
+return MakeCXType(ATT->getWrappedType(), GetTU(CT));
+
   return MakeCXType(QualType(), GetTU(CT));
 }
 
Index: clang/tools/libclang/CIndex.cpp
===
--- clang/tools/libclang/CIndex.cpp
+++ clang/tools/libclang/CIndex.cpp
@@ -1672,6 +1672,10 @@
   return Visit(TL.getModifiedLoc());
 }
 
+bool CursorVisitor::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
+  return Visit(TL.getWrappedLoc());
+}
+
 bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL,
  bool SkipResultType) {
   if (!SkipResultType && Visit(TL.getReturnLoc()))
Index: clang/test/PCH/btf_type_tag_attr.c
===
--- /dev/null
+++ clang/test/PCH/btf_type_tag_attr.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -emit-pch -o %t %s
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -include-pch %t \
+// RUN:   -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+int __attribute__((btf_type_tag("tag1"))) __attribute__((btf_type_tag("tag2"))) *p;
Index: clang/test/CodeGen/attr-btf_type_tag-similar-type.c
===
--- /dev/null
+++ clang/test/CodeGen/attr-btf_type_tag-similar-type.c
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -debug-info-kind=limited -S -emit-llvm -o - %s | FileCheck %s
+
+struct map_value {
+int __attribute__((btf_type_tag("tag1"))) __attribute__((btf_type_tag("tag3"))) *a;
+int __attribute__((btf_type_tag("tag2"))) __attribute__((btf_type_tag("tag4"))) *b;
+};
+
+struct map_value *func(void);
+
+int test(struct map_value *arg)
+{
+return *arg->a;
+}
+
+// CHECK: distinct !DICompositeType(tag: DW_TAG_structure_type, name: "map_value", file: ![[#]], line: [[#]], size: [[#]], elements: ![[L14:[0-9]+]]
+// CHECK: ![[L14]] = !{![[L15:[0-9]+]], ![[L20:[0-9]+]]}
+// CHECK: ![[L15]] = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: ![[#]], file: ![[#]], line: [[#]], baseType: ![[L16:[0-9]+]]
+// CHECK: ![[L16]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[#]], size: [[#]], annotations: ![[L17:[0-9]+]]
+// CHECK: ![[L17]] = !{![[L18:[0-9]+]], ![[L19:[0-9]+]]}
+// CHECK: ![[L18]] = !{!"btf_type_tag", !"tag1"}
+// CHECK: ![[L19]] = !{!"btf_type_tag", !"tag3"}
+// CHECK: ![[L20]] = !DIDerivedType(tag: DW_TAG_member, 

[PATCH] D120296: [Attr] Fix a btf_type_tag AST generation bug

2022-03-08 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

@erichkeane Just posted a patch to address your comments. Could you take a 
look? I will try to add more tests, esp. serialization and libclang. If you 
have any suggestions in these two areas (as I am not familiar with them), it 
would be great!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120296/new/

https://reviews.llvm.org/D120296

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120296: [Attr] Fix a btf_type_tag AST generation bug

2022-03-08 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song updated this revision to Diff 413861.
yonghong-song added a comment.

- address @erichkeane comments
- further TODO: add more tests


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120296/new/

https://reviews.llvm.org/D120296

Files:
  clang/include/clang-c/Index.h
  clang/include/clang/AST/ASTContext.h
  clang/include/clang/AST/ASTNodeTraverser.h
  clang/include/clang/AST/PropertiesBase.td
  clang/include/clang/AST/RecursiveASTVisitor.h
  clang/include/clang/AST/Type.h
  clang/include/clang/AST/TypeLoc.h
  clang/include/clang/AST/TypeProperties.td
  clang/include/clang/Basic/TypeNodes.td
  clang/include/clang/Serialization/ASTRecordReader.h
  clang/include/clang/Serialization/ASTRecordWriter.h
  clang/include/clang/Serialization/TypeBitCodes.def
  clang/lib/AST/ASTContext.cpp
  clang/lib/AST/ASTStructuralEquivalence.cpp
  clang/lib/AST/ItaniumMangle.cpp
  clang/lib/AST/TypeLoc.cpp
  clang/lib/AST/TypePrinter.cpp
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/CodeGen/CGDebugInfo.h
  clang/lib/CodeGen/CodeGenFunction.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaType.cpp
  clang/lib/Sema/TreeTransform.h
  clang/lib/Serialization/ASTReader.cpp
  clang/lib/Serialization/ASTWriter.cpp
  clang/test/CodeGen/attr-btf_type_tag-similar-type.c
  clang/tools/libclang/CIndex.cpp
  clang/tools/libclang/CXType.cpp

Index: clang/tools/libclang/CXType.cpp
===
--- clang/tools/libclang/CXType.cpp
+++ clang/tools/libclang/CXType.cpp
@@ -116,6 +116,7 @@
 TKCASE(Elaborated);
 TKCASE(Pipe);
 TKCASE(Attributed);
+TKCASE(BTFTagAttributed);
 TKCASE(Atomic);
 default:
   return CXType_Unexposed;
@@ -136,6 +137,10 @@
 return MakeCXType(ATT->getEquivalentType(), TU);
   }
 }
+if (auto *ATT = T->getAs()) {
+  if (!(TU->ParsingOptions & CXTranslationUnit_IncludeAttributedTypes))
+return MakeCXType(ATT->getWrappedType(), TU);
+}
 // Handle paren types as the original type
 if (auto *PTT = T->getAs()) {
   return MakeCXType(PTT->getInnerType(), TU);
@@ -610,6 +615,7 @@
 TKIND(Elaborated);
 TKIND(Pipe);
 TKIND(Attributed);
+TKIND(BTFTagAttributed);
 TKIND(BFloat16);
 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) TKIND(Id);
 #include "clang/Basic/OpenCLImageTypes.def"
@@ -1051,6 +1057,9 @@
   if (auto *ATT = T->getAs())
 return MakeCXType(ATT->getModifiedType(), GetTU(CT));
 
+  if (auto *ATT = T->getAs())
+return MakeCXType(ATT->getWrappedType(), GetTU(CT));
+
   return MakeCXType(QualType(), GetTU(CT));
 }
 
Index: clang/tools/libclang/CIndex.cpp
===
--- clang/tools/libclang/CIndex.cpp
+++ clang/tools/libclang/CIndex.cpp
@@ -1672,6 +1672,10 @@
   return Visit(TL.getModifiedLoc());
 }
 
+bool CursorVisitor::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
+  return Visit(TL.getWrappedLoc());
+}
+
 bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL,
  bool SkipResultType) {
   if (!SkipResultType && Visit(TL.getReturnLoc()))
Index: clang/test/CodeGen/attr-btf_type_tag-similar-type.c
===
--- /dev/null
+++ clang/test/CodeGen/attr-btf_type_tag-similar-type.c
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -debug-info-kind=limited -S -emit-llvm -o - %s | FileCheck %s
+
+struct map_value {
+int __attribute__((btf_type_tag("tag1"))) __attribute__((btf_type_tag("tag3"))) *a;
+int __attribute__((btf_type_tag("tag2"))) __attribute__((btf_type_tag("tag4"))) *b;
+};
+
+struct map_value *func(void);
+
+int test(struct map_value *arg)
+{
+return *arg->a;
+}
+
+// CHECK: distinct !DICompositeType(tag: DW_TAG_structure_type, name: "map_value", file: ![[#]], line: [[#]], size: [[#]], elements: ![[L14:[0-9]+]]
+// CHECK: ![[L14]] = !{![[L15:[0-9]+]], ![[L20:[0-9]+]]}
+// CHECK: ![[L15]] = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: ![[#]], file: ![[#]], line: [[#]], baseType: ![[L16:[0-9]+]]
+// CHECK: ![[L16]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[#]], size: [[#]], annotations: ![[L17:[0-9]+]]
+// CHECK: ![[L17]] = !{![[L18:[0-9]+]], ![[L19:[0-9]+]]}
+// CHECK: ![[L18]] = !{!"btf_type_tag", !"tag1"}
+// CHECK: ![[L19]] = !{!"btf_type_tag", !"tag3"}
+// CHECK: ![[L20]] = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: ![[#]], file: ![[#]], line: [[#]], baseType: ![[L21:[0-9]+]]
+// CHECK: ![[L21:[0-9]+]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[#]], size: [[#]], annotations: ![[L22:[0-9]+]]
+// CHECK: ![[L22]] = !{![[L23:[0-9]+]], ![[L24:[0-9]+]]}
+// CHECK: ![[L23]] = !{!"btf_type_tag", !"tag2"}
+// CHECK: ![[L24]] = !{!"btf_type_tag", !"tag4"}
Index: clang/lib/Serialization/ASTWriter.cpp

[PATCH] D120296: [Attr] Fix a btf_type_tag AST generation bug

2022-03-07 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

@erichkeane Thanks for the review. I will try to address your comments as soon 
as possible.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120296/new/

https://reviews.llvm.org/D120296

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120296: [Attr] Fix a btf_type_tag AST generation bug

2022-03-07 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added inline comments.



Comment at: clang/include/clang/AST/Type.h:4793
+
+  QualType ModifiedType;
+  QualType EquivalentType;

erichkeane wrote:
> I suspect both of these aren't necessary.  If the intent here is to just wrap 
> a single type, I think we can have only 1.  Then the fields here are just the 
> Tag, and the WrappedType.
I didn't find WrappedType in clang code base. Maybe I missed something?
But I understand what you mean. Here, unlike AttributedType which needs to deal 
with attributes which may encode as qualifiers, BTFTagAttributedType won't have 
this issue so one 'WrappedType' (representing underlying type of the btf_tag 
attribute) should be enough.



Comment at: clang/include/clang/Serialization/ASTRecordReader.h:131
 
+  StringRef readStringRef() { return readString(); }
+

erichkeane wrote:
> This is a bug.  readString returns a std::string temporary, which creating 
> the StringRef to, it now no longer exists.
Will fix.



Comment at: clang/lib/Sema/SemaType.cpp:194
+std::pair;
+SmallVector AttrsForBTFTagTypes;
+bool AttrsForBTFTagTypesSorted = true;

erichkeane wrote:
> This seems pretty absurdly expensive here... Should we instead have the 
> BTFTagAttributedType store its Attr here?
Indeed this is a good idea. This can simplify the code a lot.



Comment at: clang/lib/Sema/TreeTransform.h:6872
+  const BTFTagAttributedType *oldType = TL.getTypePtr();
+  StringRef Tag = oldType->getTag();
+  QualType modifiedType = getDerived().TransformType(TLB, TL.getModifiedLoc());

erichkeane wrote:
> Most of this tree-transform doesn't really do much, since this is a 'C' only 
> type, but otherwise we'd probably want to allow the tag itself to be 
> dependent.  
> 
> We still need this though, since there are other non-template tree-transforms.
> 
> You also might need to make this not contain a `StringRef` based on the 
> serialization issues above.
I will try to do some experiment and simplify this. Indeed this is C and non 
templates are involved.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120296/new/

https://reviews.llvm.org/D120296

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120296: [Attr] Fix a btf_type_tag AST generation bug

2022-03-05 Thread Yonghong Song via Phabricator via cfe-commits
yonghong-song added a comment.

@aaron.ballman @erichkeane Just updated the patch with a new implementation, 
which added a new BTFTagAttributedType instead of reusing existing 
AttributedType. It is quite complicated and various parts of clang are touched. 
I mostly just followed what AttributedType did but considering 
BTFTagAttributedType is C only so my current implementation may unnecessarily 
complicate things. I didn't have much test coverage either. It would be great 
you can give a look and let me know (1). what I am missing, (2). where I can 
simplify things, (3) a different approach if you see fit, (4). what additional 
tests I should have. Thanks!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120296/new/

https://reviews.llvm.org/D120296

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


  1   2   3   4   5   >