-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Clang][Attribute] Introduce maybe_undef attribute for function argum…
…ents which accepts undef values Add the ability to put __attribute__((maybe_undef)) on function arguments. Clang codegen introduces a freeze instruction on the argument. Differential Revision: https://reviews.llvm.org/D130224
- Loading branch information
Showing
9 changed files
with
291 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// RUN: %clang_cc1 -no-opaque-pointers -emit-llvm %s -o - | FileCheck %s | ||
|
||
// CHECK-LABEL: define{{.*}} void @{{.*}}test4{{.*}}(float | ||
// CHECK-NEXT: entry: | ||
// CHECK-NEXT: [[TMP2:%.*]] = alloca float, align 4 | ||
// CHECK-NEXT: store float [[TMP1:%.*]], float* [[TMP2:%.*]], align 4 | ||
// CHECK-NEXT: ret void | ||
|
||
// CHECK-LABEL: define{{.*}} void @{{.*}}test4{{.*}}(i32 | ||
// CHECK-NEXT: entry: | ||
// CHECK-NEXT: [[TMP2:%.*]] = alloca i32, align 4 | ||
// CHECK-NEXT: store i32 [[TMP1:%.*]], i32* [[TMP2:%.*]], align 4 | ||
// CHECK-NEXT: ret void | ||
|
||
// CHECK-LABEL: define{{.*}} void @{{.*}}test{{.*}}( | ||
// CHECK-NEXT: entry: | ||
// CHECK-NEXT: [[TMP1:%.*]] = alloca i32, align 4 | ||
// CHECK-NEXT: [[TMP2:%.*]] = alloca float, align 4 | ||
// CHECK-NEXT: [[TMP3:%.*]] = load i32, i32* [[TMP1:%.*]], align 4 | ||
// CHECK-NEXT: [[TMP4:%.*]] = freeze i32 [[TMP3:%.*]] | ||
// CHECK-NEXT: call void @{{.*}}test4{{.*}}(i32 noundef [[TMP4:%.*]]) | ||
// CHECK-NEXT: [[TMP5:%.*]] = load float, float* [[TMP2:%.*]], align 4 | ||
// CHECK-NEXT: [[TMP6:%.*]] = freeze float [[TMP5:%.*]] | ||
// CHECK-NEXT: call void @{{.*}}test4{{.*}}(float noundef [[TMP6:%.*]]) | ||
// CHECK-NEXT: ret void | ||
|
||
template<class T> | ||
void test4(T __attribute__((maybe_undef)) arg) { | ||
return; | ||
} | ||
|
||
template | ||
void test4<float>(float arg); | ||
|
||
template | ||
void test4<int>(int arg); | ||
|
||
void test() { | ||
int Var1; | ||
float Var2; | ||
test4<int>(Var1); | ||
test4<float>(Var2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// RUN: %clang_cc1 -no-opaque-pointers -emit-llvm %s -o - | FileCheck %s | ||
|
||
#define __maybe_undef __attribute__((maybe_undef)) | ||
|
||
// CHECK: define{{.*}} void @t1(i32 noundef [[TMP1:%.*]], i32 noundef [[TMP2:%.*]], i32 noundef [[TMP3:%.*]]) | ||
// CHECK-NEXT: entry: | ||
// CHECK-NEXT: [[TMP4:%.*]] = alloca i32, align 4 | ||
// CHECK-NEXT: [[TMP5:%.*]] = alloca i32, align 4 | ||
// CHECK-NEXT: [[TMP6:%.*]] = alloca i32, align 4 | ||
// CHECK-NEXT: store i32 [[TMP1:%.*]], i32* [[TMP4:%.*]], align 4 | ||
// CHECK-NEXT: store i32 [[TMP2:%.*]], i32* [[TMP5:%.*]], align 4 | ||
// CHECK-NEXT: store i32 [[TMP3:%.*]], i32* [[TMP6:%.*]], align 4 | ||
// CHECK-NEXT: ret void | ||
|
||
// CHECK: define{{.*}} void @t2(i32 noundef [[TMP1:%.*]], i32 noundef [[TMP2:%.*]], i32 noundef [[TMP3:%.*]]) | ||
// CHECK-NEXT: entry: | ||
// CHECK-NEXT: [[TMP4:%.*]] = alloca i32, align 4 | ||
// CHECK-NEXT: [[TMP5:%.*]] = alloca i32, align 4 | ||
// CHECK-NEXT: [[TMP6:%.*]] = alloca i32, align 4 | ||
// CHECK-NEXT: store i32 [[TMP1:%.*]], i32* [[TMP4:%.*]], align 4 | ||
// CHECK-NEXT: store i32 [[TMP2:%.*]], i32* [[TMP5:%.*]], align 4 | ||
// CHECK-NEXT: store i32 [[TMP3:%.*]], i32* [[TMP6:%.*]], align 4 | ||
// CHECK-NEXT: [[TMP7:%.*]] = load i32, i32* [[TMP4:%.*]], align 4 | ||
// CHECK-NEXT: [[TMP8:%.*]] = load i32, i32* [[TMP5:%.*]], align 4 | ||
// CHECK-NEXT: [[TMP9:%.*]] = load i32, i32* [[TMP6:%.*]], align 4 | ||
// CHECK-NEXT: [[TMP10:%.*]] = freeze i32 [[TMP8:%.*]] | ||
// CHECK-NEXT: call void @t1(i32 noundef [[TMP7:%.*]], i32 noundef [[TMP10:%.*]], i32 noundef [[TMP9:%.*]]) | ||
// CHECK-NEXT: ret void | ||
|
||
void t1(int param1, int __maybe_undef param2, int param3) {} | ||
|
||
void t2(int param1, int param2, int param3) { | ||
t1(param1, param2, param3); | ||
} | ||
|
||
// CHECK: define{{.*}} void @TestVariadicFunction(i32 noundef [[TMP0:%.*]], ...) | ||
// CHECK-NEXT: entry: | ||
// CHECK-NEXT: [[TMP1:%.*]] = alloca i32, align 4 | ||
// CHECK-NEXT: [[TMP2:%.*]] = alloca i32, align 4 | ||
// CHECK-NEXT: store i32 [[TMP0:%.*]], i32* [[TMP1:%.*]], align 4 | ||
// CHECK-NEXT: [[TMP3:%.*]] = load i32, i32* [[TMP1:%.*]], align 4 | ||
// CHECK-NEXT: [[TMP4:%.*]] = load i32, i32* [[TMP2:%.*]], align 4 | ||
// CHECK-NEXT: [[TMP5:%.*]] = load i32, i32* [[TMP2:%.*]], align 4 | ||
// CHECK-NEXT: [[TMP5:%.*]] = freeze i32 [[TMP2:%.*]] | ||
// CHECK-NEXT: call void (i32, ...) @VariadicFunction(i32 noundef [[TMP6:%.*]], i32 noundef [[TMP4:%.*]], i32 noundef [[TMP5:%.*]]) | ||
// CHECK-NEXT: ret void | ||
|
||
// CHECK: declare{{.*}} void @VariadicFunction(i32 noundef, ...) | ||
|
||
void VariadicFunction(int __maybe_undef x, ...); | ||
void TestVariadicFunction(int x, ...) { | ||
int Var; | ||
return VariadicFunction(x, Var, Var); | ||
} | ||
|
||
// CHECK: define{{.*}} void @other() | ||
// CHECK-NEXT: entry: | ||
// CHECK-NEXT: [[TMP1:%.*]] = alloca i32, align 4 | ||
// CHECK-NEXT: [[TMP2:%.*]] = load i32, i32* [[TMP1:%.*]], align 4 | ||
// CHECK-NEXT: call void @func(i32 noundef [[TMP2:%.*]]) | ||
// CHECK-NEXT: [[TMP3:%.*]] = load i32, i32* [[TMP1:%.*]], align 4 | ||
// CHECK-NEXT: [[TMP4:%.*]] = freeze i32 [[TMP3:%.*]] | ||
// CHECK-NEXT: call void @func1(i32 noundef [[TMP4:%.*]]) | ||
// CHECK-NEXT: ret void | ||
|
||
// CHECK: define{{.*}} void @func(i32 noundef [[TMP1:%.*]]) | ||
// CHECK-NEXT: entry: | ||
// CHECK-NEXT: [[TMP2:%.*]] = alloca i32, align 4 | ||
// CHECK-NEXT: store i32 [[TMP1:%.*]], i32* [[TMP2:%.*]], align 4 | ||
// CHECK-NEXT: ret void | ||
|
||
// CHECK: define{{.*}} void @func1(i32 noundef [[TMP1:%.*]]) | ||
// CHECK-NEXT: entry: | ||
// CHECK-NEXT: [[TMP2:%.*]] = alloca i32, align 4 | ||
// CHECK-NEXT: store i32 [[TMP1:%.*]], i32* [[TMP2:%.*]], align 4 | ||
// CHECK-NEXT: ret void | ||
|
||
void func(int param); | ||
void func1(int __maybe_undef param); | ||
|
||
void other() { | ||
int Var; | ||
func(Var); | ||
func1(Var); | ||
} | ||
|
||
void func(__maybe_undef int param) {} | ||
void func1(int param) {} | ||
|
||
// CHECK: define{{.*}} void @foo(i32 noundef [[TMP1:%.*]]) | ||
// CHECK-NEXT: entry: | ||
// CHECK-NEXT: [[TMP2:%.*]] = alloca i32, align 4 | ||
// CHECK-NEXT: store i32 [[TMP1:%.*]], i32* [[TMP2:%.*]], align 4 | ||
// CHECK-NEXT: ret void | ||
|
||
// CHECK: define{{.*}} void @bar() | ||
// CHECK-NEXT: entry: | ||
// CHECK-NEXT: [[TMP1:%.*]] = alloca i32, align 4 | ||
// CHECK-NEXT: [[TMP2:%.*]] = load i32, i32* [[TMP1:%.*]], align 4 | ||
// CHECK-NEXT: call void @foo(i32 noundef [[TMP2:%.*]]) | ||
// CHECK-NEXT: ret void | ||
|
||
void foo(__maybe_undef int param); | ||
void foo(int param) {} | ||
|
||
void bar() { | ||
int Var; | ||
foo(Var); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// RUN: %clang_cc1 -no-opaque-pointers -triple amdgcn-amd-amdhsa -target-cpu gfx906 -x hip -fcuda-is-device -emit-llvm %s \ | ||
// RUN: -o - | FileCheck %s | ||
|
||
// CHECK: define dso_local amdgpu_kernel void @_Z13shufflekernelv() | ||
// CHECK-NEXT: entry: | ||
// CHECK-NEXT: [[TMP1:%.*]] = alloca i32, align 4, addrspace(5) | ||
// CHECK-NEXT: [[TMP2:%.*]] = alloca i32, align 4, addrspace(5) | ||
// CHECK-NEXT: [[TMP3:%.*]] = addrspacecast i32 addrspace(5)* [[TMP1:%.*]] to i32* | ||
// CHECK-NEXT: [[TMP4:%.*]] = addrspacecast i32 addrspace(5)* [[TMP2:%.*]] to i32* | ||
// CHECK-NEXT: [[TMP5:%.*]] = load i32, i32* [[TMP3:%.*]], align 4 | ||
// CHECK-NEXT: [[TMP6:%.*]] = freeze i32 [[TMP5:%.*]] | ||
// CHECK-NEXT: %call = call noundef i32 @_Z11__shfl_synciii(i32 noundef [[TMP6:%.*]], i32 noundef 64, i32 noundef 0) #4 | ||
// CHECK-NEXT: store i32 %call, i32* [[TMP4:%.*]], align 4 | ||
// CHECK-NEXT: ret void | ||
|
||
// CHECK: define linkonce_odr noundef i32 @_Z11__shfl_synciii(i32 noundef [[TMP1:%.*]], i32 noundef [[TMP2:%.*]], i32 noundef [[TMP3:%.*]]) | ||
|
||
#define __global__ __attribute__((global)) | ||
#define __device__ __attribute__((device)) | ||
#define __maybe_undef __attribute__((maybe_undef)) | ||
#define WARP_SIZE 64 | ||
|
||
static constexpr int warpSize = __AMDGCN_WAVEFRONT_SIZE; | ||
|
||
__device__ static inline unsigned int __lane_id() { | ||
return __builtin_amdgcn_mbcnt_hi( | ||
-1, __builtin_amdgcn_mbcnt_lo(-1, 0)); | ||
} | ||
|
||
__device__ | ||
inline | ||
int __shfl_sync(int __maybe_undef var, int src_lane, int width = warpSize) { | ||
int self = __lane_id(); | ||
int index = src_lane + (self & ~(width-1)); | ||
return __builtin_amdgcn_ds_bpermute(index<<2, var); | ||
} | ||
|
||
__global__ void | ||
shufflekernel() | ||
{ | ||
int t; | ||
int res; | ||
res = __shfl_sync(t, WARP_SIZE, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// RUN: %clang_cc1 -fsyntax-only -verify %s | ||
|
||
// Decl annotations. | ||
void f(int __attribute__((maybe_undef)) *a); | ||
void (*fp)(int __attribute__((maybe_undef)) handle); | ||
__attribute__((maybe_undef)) int i(); // expected-warning {{'maybe_undef' attribute only applies to parameters}} | ||
int __attribute__((maybe_undef)) a; // expected-warning {{'maybe_undef' attribute only applies to parameters}} | ||
int (* __attribute__((maybe_undef)) fpt)(char *); // expected-warning {{'maybe_undef' attribute only applies to parameters}} | ||
void h(int *a __attribute__((maybe_undef("RandomString")))); // expected-error {{'maybe_undef' attribute takes no arguments}} | ||
|
||
// Type annotations. | ||
int __attribute__((maybe_undef)) ta; // expected-warning {{'maybe_undef' attribute only applies to parameters}} | ||
|
||
// Typedefs. | ||
typedef int callback(char *) __attribute__((maybe_undef)); // expected-warning {{'maybe_undef' attribute only applies to parameters}} |