Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HLSL] Change default linkage of HLSL functions and groupshared variables #93336

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions clang/docs/HLSL/ExpectedDifferences.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,16 @@ behavior between Clang and DXC. Some examples include:
diagnostic notifying the user of the conversion rather than silently altering
precision relative to the other overloads (as FXC does) or generating code
that will fail validation (as DXC does).

Correctness improvements (bug fixes)
====================================

Entry point functions & ``static`` keyword
------------------------------------------
Marking a shader entry point function ``static`` will result in an error.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a test for this already?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes:

// expected-warning@+1 {{'shader' attribute only applies to global functions}}
[shader("vertex")]
static void oops() {}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. Is it significant that these are warnings, while the rst file says errors?


This is identical to DXC behavior when an entry point is specified as compiler
argument. However, DXC does not report an error when compiling a shader library
that has an entry point function with ``[shader("stage")]`` attribute that is
also marked ``static``. Additionally, this function definition is not included
in the final DXIL.
6 changes: 6 additions & 0 deletions clang/lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ LinkageComputer::getLVForNamespaceScopeDecl(const NamedDecl *D,
// - a variable, variable template, function, or function template
// that is explicitly declared static; or
// (This bullet corresponds to C99 6.2.2p3.)
// - also applies to HLSL
return LinkageInfo::internal();
}

Expand Down Expand Up @@ -657,6 +658,11 @@ LinkageComputer::getLVForNamespaceScopeDecl(const NamedDecl *D,
if (PrevVar->getStorageClass() == SC_Static)
return LinkageInfo::internal();
}

if (Context.getLangOpts().HLSL &&
Var->hasAttr<HLSLGroupSharedAddressSpaceAttr>())
return LinkageInfo::internal();

} else if (const auto *IFD = dyn_cast<IndirectFieldDecl>(D)) {
// - a data member of an anonymous union.
const VarDecl *VD = IFD->getVarDecl();
Expand Down
15 changes: 15 additions & 0 deletions clang/lib/CodeGen/CGHLSLRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,21 @@ llvm::Value *CGHLSLRuntime::emitInputSemantic(IRBuilder<> &B,
return nullptr;
}

void CGHLSLRuntime::emitFunctionProlog(const FunctionDecl *FD,
llvm::Function *Fn) {
if (!FD || !Fn)
return;

if (FD->hasAttr<HLSLShaderAttr>()) {
emitEntryFunction(FD, Fn);
} else {
// HLSL functions defined in the current translation unit that are not
// shader entry points or exported have internal linkage by default.
if (FD->isDefined())
Fn->setLinkage(GlobalValue::InternalLinkage);
}
}

void CGHLSLRuntime::emitEntryFunction(const FunctionDecl *FD,
llvm::Function *Fn) {
llvm::Module &M = CGM.getModule();
Expand Down
7 changes: 3 additions & 4 deletions clang/lib/CodeGen/CGHLSLRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,11 @@ class CGHLSLRuntime {
void addBuffer(const HLSLBufferDecl *D);
void finishCodeGen();

void setHLSLEntryAttributes(const FunctionDecl *FD, llvm::Function *Fn);

void emitEntryFunction(const FunctionDecl *FD, llvm::Function *Fn);
void setHLSLFunctionAttributes(llvm::Function *, const FunctionDecl *);
void emitFunctionProlog(const FunctionDecl *FD, llvm::Function *Fn);

private:
void emitEntryFunction(const FunctionDecl *FD, llvm::Function *Fn);
void setHLSLEntryAttributes(const FunctionDecl *FD, llvm::Function *Fn);
void addBufferResourceAnnotation(llvm::GlobalVariable *GV,
llvm::hlsl::ResourceClass RC,
llvm::hlsl::ResourceKind RK, bool IsROV,
Expand Down
7 changes: 4 additions & 3 deletions clang/lib/CodeGen/CodeGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1194,9 +1194,10 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
if (getLangOpts().OpenMP && CurCodeDecl)
CGM.getOpenMPRuntime().emitFunctionProlog(*this, CurCodeDecl);

// Handle emitting HLSL entry functions.
if (D && D->hasAttr<HLSLShaderAttr>())
CGM.getHLSLRuntime().emitEntryFunction(FD, Fn);
// Emit HLSL specific initialization
if (getLangOpts().HLSL) {
CGM.getHLSLRuntime().emitFunctionProlog(FD, Fn);
}

EmitFunctionProlog(*CurFnInfo, CurFn, Args);

Expand Down
12 changes: 6 additions & 6 deletions clang/test/CodeGenHLSL/ArrayTemporary.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

void fn(float x[2]) { }

// CHECK-LABEL: define void {{.*}}call{{.*}}
// CHECK-LABEL: define internal void {{.*}}call{{.*}}
// CHECK: [[Arr:%.*]] = alloca [2 x float]
// CHECK: [[Tmp:%.*]] = alloca [2 x float]
// CHECK: call void @llvm.memset.p0.i32(ptr align 4 [[Arr]], i8 0, i32 8, i1 false)
Expand All @@ -20,7 +20,7 @@ struct Obj {

void fn2(Obj O[4]) { }

// CHECK-LABEL: define void {{.*}}call2{{.*}}
// CHECK-LABEL: define internal void {{.*}}call2{{.*}}
// CHECK: [[Arr:%.*]] = alloca [4 x %struct.Obj]
// CHECK: [[Tmp:%.*]] = alloca [4 x %struct.Obj]
// CHECK: call void @llvm.memset.p0.i32(ptr align 4 [[Arr]], i8 0, i32 32, i1 false)
Expand All @@ -34,7 +34,7 @@ void call2() {

void fn3(float x[2][2]) { }

// CHECK-LABEL: define void {{.*}}call3{{.*}}
// CHECK-LABEL: define internal void {{.*}}call3{{.*}}
// CHECK: [[Arr:%.*]] = alloca [2 x [2 x float]]
// CHECK: [[Tmp:%.*]] = alloca [2 x [2 x float]]
// CHECK: call void @llvm.memcpy.p0.p0.i32(ptr align 4 [[Arr]], ptr align 4 {{.*}}, i32 16, i1 false)
Expand All @@ -45,7 +45,7 @@ void call3() {
fn3(Arr);
}

// CHECK-LABEL: define void {{.*}}call4{{.*}}(ptr
// CHECK-LABEL: define internal void {{.*}}call4{{.*}}(ptr
// CHECK-SAME: noundef byval([2 x [2 x float]]) align 4 [[Arr:%.*]])
// CHECK: [[Tmp:%.*]] = alloca [2 x [2 x float]]
// CHECK: call void @llvm.memcpy.p0.p0.i32(ptr align 4 [[Tmp]], ptr align 4 [[Arr]], i32 16, i1 false)
Expand All @@ -58,7 +58,7 @@ void call4(float Arr[2][2]) {
// Verify that each template instantiation codegens to a unique and correctly
// mangled function name.

// CHECK-LABEL: define void {{.*}}template_call{{.*}}(ptr
// CHECK-LABEL: define internal void {{.*}}template_call{{.*}}(ptr

// CHECK-SAME: noundef byval([2 x float]) align 4 [[FA2:%[0-9A-Z]+]],
// CHECK-SAME: ptr noundef byval([4 x float]) align 4 [[FA4:%[0-9A-Z]+]],
Expand All @@ -85,7 +85,7 @@ void template_call(float FA2[2], float FA4[4], int IA3[3]) {


// Verify that Array parameter element access correctly codegens.
// CHECK-LABEL: define void {{.*}}element_access{{.*}}(ptr
// CHECK-LABEL: define internal void {{.*}}element_access{{.*}}(ptr
// CHECK-SAME: noundef byval([2 x float]) align 4 [[FA2:%[0-9A-Z]+]]

// CHECK: [[Addr:%.*]] = getelementptr inbounds [2 x float], ptr [[FA2]], i32 0, i32 0
Expand Down
56 changes: 28 additions & 28 deletions clang/test/CodeGenHLSL/builtins/abs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,85 +9,85 @@
using hlsl::abs;

#ifdef __HLSL_ENABLE_16_BIT
// NATIVE_HALF: define noundef i16 @
// NATIVE_HALF: define internal noundef i16 @
// NATIVE_HALF: call i16 @llvm.abs.i16(
int16_t test_abs_int16_t(int16_t p0) { return abs(p0); }
// NATIVE_HALF: define noundef <2 x i16> @
// NATIVE_HALF: define internal noundef <2 x i16> @
// NATIVE_HALF: call <2 x i16> @llvm.abs.v2i16(
int16_t2 test_abs_int16_t2(int16_t2 p0) { return abs(p0); }
// NATIVE_HALF: define noundef <3 x i16> @
// NATIVE_HALF: define internal noundef <3 x i16> @
// NATIVE_HALF: call <3 x i16> @llvm.abs.v3i16(
int16_t3 test_abs_int16_t3(int16_t3 p0) { return abs(p0); }
// NATIVE_HALF: define noundef <4 x i16> @
// NATIVE_HALF: define internal noundef <4 x i16> @
// NATIVE_HALF: call <4 x i16> @llvm.abs.v4i16(
int16_t4 test_abs_int16_t4(int16_t4 p0) { return abs(p0); }
#endif // __HLSL_ENABLE_16_BIT

// NATIVE_HALF: define noundef half @
// NATIVE_HALF: define internal noundef half @
// NATIVE_HALF: call half @llvm.fabs.f16(
// NO_HALF: define noundef float @"?test_abs_half@@YA$halff@$halff@@Z"(
// NO_HALF: define internal noundef float @"?test_abs_half@@YA$halff@$halff@@Z"(
// NO_HALF: call float @llvm.fabs.f32(float %0)
half test_abs_half(half p0) { return abs(p0); }
// NATIVE_HALF: define noundef <2 x half> @
// NATIVE_HALF: define internal noundef <2 x half> @
// NATIVE_HALF: call <2 x half> @llvm.fabs.v2f16(
// NO_HALF: define noundef <2 x float> @"?test_abs_half2@@YAT?$__vector@$halff@$01@__clang@@T12@@Z"(
// NO_HALF: define internal noundef <2 x float> @"?test_abs_half2@@YAT?$__vector@$halff@$01@__clang@@T12@@Z"(
// NO_HALF: call <2 x float> @llvm.fabs.v2f32(
half2 test_abs_half2(half2 p0) { return abs(p0); }
// NATIVE_HALF: define noundef <3 x half> @
// NATIVE_HALF: define internal noundef <3 x half> @
// NATIVE_HALF: call <3 x half> @llvm.fabs.v3f16(
// NO_HALF: define noundef <3 x float> @"?test_abs_half3@@YAT?$__vector@$halff@$02@__clang@@T12@@Z"(
// NO_HALF: define internal noundef <3 x float> @"?test_abs_half3@@YAT?$__vector@$halff@$02@__clang@@T12@@Z"(
// NO_HALF: call <3 x float> @llvm.fabs.v3f32(
half3 test_abs_half3(half3 p0) { return abs(p0); }
// NATIVE_HALF: define noundef <4 x half> @
// NATIVE_HALF: define internal noundef <4 x half> @
// NATIVE_HALF: call <4 x half> @llvm.fabs.v4f16(
// NO_HALF: define noundef <4 x float> @"?test_abs_half4@@YAT?$__vector@$halff@$03@__clang@@T12@@Z"(
// NO_HALF: define internal noundef <4 x float> @"?test_abs_half4@@YAT?$__vector@$halff@$03@__clang@@T12@@Z"(
// NO_HALF: call <4 x float> @llvm.fabs.v4f32(
half4 test_abs_half4(half4 p0) { return abs(p0); }
// CHECK: define noundef i32 @
// CHECK: define internal noundef i32 @
// CHECK: call i32 @llvm.abs.i32(
int test_abs_int(int p0) { return abs(p0); }
// CHECK: define noundef <2 x i32> @
// CHECK: define internal noundef <2 x i32> @
// CHECK: call <2 x i32> @llvm.abs.v2i32(
int2 test_abs_int2(int2 p0) { return abs(p0); }
// CHECK: define noundef <3 x i32> @
// CHECK: define internal noundef <3 x i32> @
// CHECK: call <3 x i32> @llvm.abs.v3i32(
int3 test_abs_int3(int3 p0) { return abs(p0); }
// CHECK: define noundef <4 x i32> @
// CHECK: define internal noundef <4 x i32> @
// CHECK: call <4 x i32> @llvm.abs.v4i32(
int4 test_abs_int4(int4 p0) { return abs(p0); }
// CHECK: define noundef float @
// CHECK: define internal noundef float @
// CHECK: call float @llvm.fabs.f32(
float test_abs_float(float p0) { return abs(p0); }
// CHECK: define noundef <2 x float> @
// CHECK: define internal noundef <2 x float> @
// CHECK: call <2 x float> @llvm.fabs.v2f32(
float2 test_abs_float2(float2 p0) { return abs(p0); }
// CHECK: define noundef <3 x float> @
// CHECK: define internal noundef <3 x float> @
// CHECK: call <3 x float> @llvm.fabs.v3f32(
float3 test_abs_float3(float3 p0) { return abs(p0); }
// CHECK: define noundef <4 x float> @
// CHECK: define internal noundef <4 x float> @
// CHECK: call <4 x float> @llvm.fabs.v4f32(
float4 test_abs_float4(float4 p0) { return abs(p0); }
// CHECK: define noundef i64 @
// CHECK: define internal noundef i64 @
// CHECK: call i64 @llvm.abs.i64(
int64_t test_abs_int64_t(int64_t p0) { return abs(p0); }
// CHECK: define noundef <2 x i64> @
// CHECK: define internal noundef <2 x i64> @
// CHECK: call <2 x i64> @llvm.abs.v2i64(
int64_t2 test_abs_int64_t2(int64_t2 p0) { return abs(p0); }
// CHECK: define noundef <3 x i64> @
// CHECK: define internal noundef <3 x i64> @
// CHECK: call <3 x i64> @llvm.abs.v3i64(
int64_t3 test_abs_int64_t3(int64_t3 p0) { return abs(p0); }
// CHECK: define noundef <4 x i64> @
// CHECK: define internal noundef <4 x i64> @
// CHECK: call <4 x i64> @llvm.abs.v4i64(
int64_t4 test_abs_int64_t4(int64_t4 p0) { return abs(p0); }
// CHECK: define noundef double @
// CHECK: define internal noundef double @
// CHECK: call double @llvm.fabs.f64(
double test_abs_double(double p0) { return abs(p0); }
// CHECK: define noundef <2 x double> @
// CHECK: define internal noundef <2 x double> @
// CHECK: call <2 x double> @llvm.fabs.v2f64(
double2 test_abs_double2(double2 p0) { return abs(p0); }
// CHECK: define noundef <3 x double> @
// CHECK: define internal noundef <3 x double> @
// CHECK: call <3 x double> @llvm.fabs.v3f64(
double3 test_abs_double3(double3 p0) { return abs(p0); }
// CHECK: define noundef <4 x double> @
// CHECK: define internal noundef <4 x double> @
// CHECK: call <4 x double> @llvm.fabs.v4f64(
double4 test_abs_double4(double4 p0) { return abs(p0); }
Loading
Loading