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(&map1, cb, &cb_data, 0);
} else {
bpf_for_each_map_elem(&map2, cb, &cb_data, 0);
}
Is converted to bytecode equivalent to:
if (data_end - data > 1024)
tmp = &map1;
else
tmp = &map2;
bpf_for_each_map_elem(tmp, cb, &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-76dd9c8318a4@meta.com/
Differential Revision: https://reviews.llvm.org/D152986
104 lines
2.8 KiB
C++
104 lines
2.8 KiB
C++
// RUN: %clang_cc1 -S -emit-llvm %s -triple x86_64-unknown-linux-gnu -o - | FileCheck %s
|
|
|
|
class A {
|
|
public:
|
|
[[clang::nomerge]] A();
|
|
[[clang::nomerge]] virtual ~A();
|
|
[[clang::nomerge]] void f();
|
|
[[clang::nomerge]] virtual void g();
|
|
[[clang::nomerge]] static void f1();
|
|
};
|
|
|
|
class B : public A {
|
|
public:
|
|
void g() override;
|
|
};
|
|
|
|
bool bar();
|
|
[[clang::nomerge]] void f(bool, bool);
|
|
[[clang::nomerge]] void (*fptr)(void);
|
|
|
|
void foo(int i, A *ap, B *bp) {
|
|
[[clang::nomerge]] bar();
|
|
[[clang::nomerge]] (i = 4, bar());
|
|
[[clang::nomerge]] (void)(bar());
|
|
f(bar(), bar());
|
|
fptr();
|
|
[[clang::nomerge]] [] { bar(); bar(); }(); // nomerge only applies to the anonymous function call
|
|
[[clang::nomerge]] for (bar(); bar(); bar()) {}
|
|
[[clang::nomerge]] { asm("nop"); }
|
|
bar();
|
|
|
|
ap->g();
|
|
bp->g();
|
|
|
|
A a;
|
|
a.f();
|
|
a.g();
|
|
A::f1();
|
|
|
|
B b;
|
|
b.g();
|
|
|
|
A *newA = new B();
|
|
delete newA;
|
|
}
|
|
|
|
int g(int i);
|
|
|
|
void something() {
|
|
g(1);
|
|
}
|
|
|
|
[[clang::nomerge]] int g(int i);
|
|
|
|
void something_else() {
|
|
g(1);
|
|
}
|
|
|
|
int g(int i) { return i; }
|
|
|
|
void something_else_again() {
|
|
g(1);
|
|
}
|
|
|
|
// CHECK: call noundef zeroext i1 @_Z3barv() #[[ATTR0:[0-9]+]]
|
|
// CHECK: call noundef zeroext i1 @_Z3barv() #[[ATTR0]]
|
|
// CHECK: call noundef zeroext i1 @_Z3barv() #[[ATTR0]]
|
|
// CHECK: call noundef zeroext i1 @_Z3barv(){{$}}
|
|
// CHECK: call noundef zeroext i1 @_Z3barv(){{$}}
|
|
// CHECK: call void @_Z1fbb({{.*}}) #[[ATTR0]]
|
|
// CHECK: %[[FPTR:.*]] = load ptr, ptr @fptr
|
|
// CHECK-NEXT: call void %[[FPTR]]() #[[ATTR0]]
|
|
// CHECK: call void @"_ZZ3fooiP1AP1BENK3$_0clEv"{{.*}} #[[ATTR0]]
|
|
// CHECK: call noundef zeroext i1 @_Z3barv() #[[ATTR0]]
|
|
// CHECK-LABEL: for.cond:
|
|
// CHECK: call noundef zeroext i1 @_Z3barv() #[[ATTR0]]
|
|
// CHECK-LABEL: for.inc:
|
|
// CHECK: call noundef zeroext i1 @_Z3barv() #[[ATTR0]]
|
|
// CHECK: call void asm sideeffect "nop"{{.*}} #[[ATTR1:[0-9]+]]
|
|
// CHECK: call noundef zeroext i1 @_Z3barv(){{$}}
|
|
// CHECK: load ptr, ptr
|
|
// CHECK: load ptr, ptr
|
|
// CHECK: %[[AG:.*]] = load ptr, ptr
|
|
// CHECK-NEXT: call void %[[AG]](ptr {{.*}}) #[[ATTR0]]
|
|
// CHECK: load ptr, ptr
|
|
// CHECK: load ptr, ptr
|
|
// CHECK: %[[BG:.*]] = load ptr, ptr
|
|
// CHECK-NEXT: call void %[[BG]](ptr noundef{{.*}}
|
|
// CHECK: call void @_ZN1AC1Ev({{.*}}) #[[ATTR0]]
|
|
// CHECK: call void @_ZN1A1fEv({{.*}}) #[[ATTR0]]
|
|
// CHECK: call void @_ZN1A1gEv({{.*}}) #[[ATTR0]]
|
|
// CHECK: call void @_ZN1A2f1Ev() #[[ATTR0]]
|
|
// CHECK: call void @_ZN1BC1Ev({{.*}}){{$}}
|
|
// CHECK: call void @_ZN1B1gEv({{.*}}){{$}}
|
|
// CHECK: call void @_ZN1BC1Ev({{.*}}){{$}}
|
|
// CHECK: load ptr, ptr
|
|
// CHECK: load ptr, ptr
|
|
// CHECK: %[[AG:.*]] = load ptr, ptr
|
|
// CHECK-NEXT: call void %[[AG]](ptr {{.*}}) #[[ATTR1]]
|
|
// CHECK: call void @_ZN1AD1Ev(ptr {{.*}}) #[[ATTR1]]
|
|
|
|
// CHECK-DAG: attributes #[[ATTR0]] = {{{.*}}nomerge{{.*}}}
|
|
// CHECK-DAG: attributes #[[ATTR1]] = {{{.*}}nomerge{{.*}}}
|