[clang] 06eee73 - [clang] Allow 'nomerge' attribute for function pointers

2023-06-26 Thread Eduard Zingerman via cfe-commits

Author: Eduard Zingerman
Date: 2023-06-27T01:15:45+03:00
New Revision: 06eee734c1ea9de754c1e7e8c79c0897b9e01350

URL: 
https://github.com/llvm/llvm-project/commit/06eee734c1ea9de754c1e7e8c79c0897b9e01350
DIFF: 
https://github.com/llvm/llvm-project/commit/06eee734c1ea9de754c1e7e8c79c0897b9e01350.diff

LOG: [clang] Allow 'nomerge' attribute for function pointers

Allow specifying 'nomerge' attribute for function pointers,
e.g. like in the following C code:

extern void (*foo)(void) __attribute__((nomerge));
void bar(long i) {
  if (i)
foo();
  else
foo();
}

With the goal to attach 'nomerge' to both calls done through 'foo':

@foo = external local_unnamed_addr global ptr, align 8
define dso_local void @bar(i64 noundef %i) local_unnamed_addr #0 {
  ; ...
  %0 = load ptr, ptr @foo, align 8, !tbaa !5
  ; ...
if.then:
  tail call void %0() #1
  br label %if.end
if.else:
  tail call void %0() #1
  br label %if.end
if.end:
  ret void
}
; ...
attributes #1 = { nomerge ... }

Report a warning in case if 'nomerge' is specified for a variable that
is not a function pointer, e.g.:

t.c:2:22: warning: 'nomerge' attribute is ignored because 'j' is not a 
function pointer [-Wignored-attributes]
2 | int j __attribute__((nomerge));
  |  ^

The intended use-case is for BPF backend.

BPF provides a sort of "standard library" functions that are called
helpers. BPF also verifies usage of these helpers before program
execution. Because of limitations of verification / runtime model it
is important to keep calls to some of such helpers from merging.

An example could be found by the link [1], there input C code:

 if (data_end - data > 1024) {
 bpf_for_each_map_elem(, cb, _data, 0);
 } else {
 bpf_for_each_map_elem(, cb, _data, 0);
 }

Is converted to bytecode equivalent to:

 if (data_end - data > 1024)
   tmp = 
 else
   tmp = 
 bpf_for_each_map_elem(tmp, cb, _data, 0);

However, BPF verification/runtime requires to use the same map address
for each particular `bpf_for_each_map_elem()` call.

The 'nomerge' attribute is a perfect match for this situation, but
unfortunately BPF helpers are declared as pointers to functions:

static long (*bpf_for_each_map_elem)(void *map, ...) = (void *) 164;

Hence, this commit, allowing to use 'nomerge' for function pointers.

[1] https://lore.kernel.org/bpf/03bdf90f-f374-1e67-69d6-76dd9c831...@meta.com/

Differential Revision: https://reviews.llvm.org/D152986

Added: 


Modified: 
clang/include/clang/Basic/Attr.td
clang/include/clang/Basic/AttrDocs.td
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/CodeGen/CGCall.cpp
clang/lib/Sema/SemaDeclAttr.cpp
clang/test/CodeGen/attr-nomerge.cpp
clang/test/Misc/pragma-attribute-supported-attributes-list.test
clang/test/Parser/stmt-attributes.c
clang/test/Parser/stmt-attributes.cpp
clang/test/Parser/stmt-attributes.m
clang/test/Sema/attr-nomerge-ast.cpp
clang/test/Sema/attr-nomerge.cpp

Removed: 




diff  --git a/clang/include/clang/Basic/Attr.td 
b/clang/include/clang/Basic/Attr.td
index b9e701641fdf1..d5204b2869667 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -1482,9 +1482,8 @@ def : MutualExclusions<[Likely, Unlikely]>;
 def NoMerge : DeclOrStmtAttr {
   let Spellings = [Clang<"nomerge">];
   let Documentation = [NoMergeDocs];
-  let Subjects = SubjectList<[Function, Stmt], ErrorDiag,
- "functions and statements">;
-  let SimpleHandler = 1;
+  let Subjects = SubjectList<[Function, Stmt, Var], ErrorDiag,
+ "functions, statements and variables">;
 }
 
 def MustTail : StmtAttr {

diff  --git a/clang/include/clang/Basic/AttrDocs.td 
b/clang/include/clang/Basic/AttrDocs.td
index 5178ca43b9710..2c950231255d7 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -549,7 +549,31 @@ over the tradeoff between code size and debug information 
precision.
 
 ``nomerge`` attribute can also be used as function attribute to prevent all
 calls to the specified function from merging. It has no effect on indirect
-calls.
+calls to such functions. For example:
+
+.. code-block:: c++
+
+  [[clang::nomerge]] void foo(int) {}
+
+  void bar(int x) {
+auto *ptr = foo;
+if (x) foo(1); else foo(2); // will not be merged
+if (x) ptr(1); else ptr(2); // indirect call, can be merged
+  }
+
+``nomerge`` attribute can also be used for pointers to functions to
+prevent calls through such pointer from merging. In such case the
+effect applies only to a specific function pointer. For example:
+
+.. code-block:: c++
+
+  [[clang::nomerge]] void (*foo)(int);
+
+  void bar(int x) {
+auto *ptr = 

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

2023-03-15 Thread Eduard Zingerman via cfe-commits
On Wed, 2023-03-15 at 17:10 +0100, Jose E. Marchesi wrote:
> > Could you please take a look to verify that this implementation is on
> > the same page with what is planned for GCC?
> 
> Sure.  Can you please add David Faust as a subscriber as well?  I don't
> know if he has an account in reviews.llvm.org.

I wanted to, but have not found his account for reviews.llvm.org.
And Phabricator interface does not seem to allow to subscribe non-user
emails, unfortunately.
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits