diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp index 6cccd353cff96..03ddc87d8d3df 100644 --- a/clang/lib/CodeGen/CGHLSLRuntime.cpp +++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp @@ -54,10 +54,6 @@ void addDxilValVersion(StringRef ValVersionStr, llvm::Module &M) { auto *DXILValMD = M.getOrInsertNamedMetadata(DXILValKey); DXILValMD->addOperand(Val); } -void addDisableOptimizations(llvm::Module &M) { - StringRef Key = "dx.disable_optimizations"; - M.addModuleFlag(llvm::Module::ModFlagBehavior::Override, Key, 1); -} // cbuffer will be translated into global variable in special address space. // If translate into C, // cbuffer A { @@ -171,8 +167,6 @@ void CGHLSLRuntime::finishCodeGen() { addDxilValVersion(TargetOpts.DxilValidatorVersion, M); generateGlobalCtorDtorCalls(); - if (CGM.getCodeGenOpts().OptimizationLevel == 0) - addDisableOptimizations(M); const DataLayout &DL = M.getDataLayout(); diff --git a/clang/test/CodeGenHLSL/disable_opt.hlsl b/clang/test/CodeGenHLSL/disable_opt.hlsl deleted file mode 100644 index bfffe76cfa9de..0000000000000 --- a/clang/test/CodeGenHLSL/disable_opt.hlsl +++ /dev/null @@ -1,12 +0,0 @@ -// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -O0 -emit-llvm -xhlsl -o - %s | FileCheck %s -// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -O3 -emit-llvm -xhlsl -o - %s | FileCheck %s --check-prefix=OPT - -// CHECK:!"dx.disable_optimizations", i32 1} - -// OPT-NOT:"dx.disable_optimizations" - -float bar(float a, float b); - -float foo(float a, float b) { - return bar(a, b); -} diff --git a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp index 6a15bac153d85..453aacf49abb3 100644 --- a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp +++ b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp @@ -17,6 +17,8 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/Analysis/CallGraph.h" #include "llvm/Analysis/DXILResource.h" +#include "llvm/IR/Attributes.h" +#include "llvm/IR/DiagnosticInfo.h" #include "llvm/IR/Instruction.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/IntrinsicInst.h" @@ -96,7 +98,8 @@ void ModuleShaderFlags::updateFunctionFlags(ComputedShaderFlags &CSF, } /// Construct ModuleShaderFlags for module Module M -void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM) { +void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM, + const ModuleMetadataInfo &MMDI) { CallGraph CG(M); // Compute Shader Flags Mask for all functions using post-order visit of SCC @@ -142,6 +145,20 @@ void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM) { // Merge SCCSF with that of F FunctionFlags[F].merge(SCCSF); } + + // Set DisableOptimizations flag based on the presence of OptimizeNone + // attribute of entry functions. + if (MMDI.EntryPropertyVec.size() > 0) { + CombinedSFMask.DisableOptimizations = + MMDI.EntryPropertyVec[0].Entry->hasFnAttribute( + llvm::Attribute::OptimizeNone); + // Ensure all entry functions have the same optimization attribute + for (const auto &EntryFunProps : MMDI.EntryPropertyVec) + if (CombinedSFMask.DisableOptimizations != + EntryFunProps.Entry->hasFnAttribute(llvm::Attribute::OptimizeNone)) + EntryFunProps.Entry->getContext().diagnose(DiagnosticInfoUnsupported( + *(EntryFunProps.Entry), "Inconsistent optnone attribute ")); + } } void ComputedShaderFlags::print(raw_ostream &OS) const { @@ -180,9 +197,10 @@ AnalysisKey ShaderFlagsAnalysis::Key; ModuleShaderFlags ShaderFlagsAnalysis::run(Module &M, ModuleAnalysisManager &AM) { DXILResourceTypeMap &DRTM = AM.getResult(M); + const ModuleMetadataInfo MMDI = AM.getResult(M); ModuleShaderFlags MSFI; - MSFI.initialize(M, DRTM); + MSFI.initialize(M, DRTM, MMDI); return MSFI; } @@ -212,14 +230,17 @@ PreservedAnalyses ShaderFlagsAnalysisPrinter::run(Module &M, bool ShaderFlagsAnalysisWrapper::runOnModule(Module &M) { DXILResourceTypeMap &DRTM = getAnalysis().getResourceTypeMap(); + const ModuleMetadataInfo MMDI = + getAnalysis().getModuleMetadata(); - MSFI.initialize(M, DRTM); + MSFI.initialize(M, DRTM, MMDI); return false; } void ShaderFlagsAnalysisWrapper::getAnalysisUsage(AnalysisUsage &AU) const { AU.setPreservesAll(); AU.addRequiredTransitive(); + AU.addRequired(); } char ShaderFlagsAnalysisWrapper::ID = 0; @@ -227,5 +248,6 @@ char ShaderFlagsAnalysisWrapper::ID = 0; INITIALIZE_PASS_BEGIN(ShaderFlagsAnalysisWrapper, "dx-shader-flag-analysis", "DXIL Shader Flag Analysis", true, true) INITIALIZE_PASS_DEPENDENCY(DXILResourceTypeWrapperPass) +INITIALIZE_PASS_DEPENDENCY(DXILMetadataAnalysisWrapperPass) INITIALIZE_PASS_END(ShaderFlagsAnalysisWrapper, "dx-shader-flag-analysis", "DXIL Shader Flag Analysis", true, true) diff --git a/llvm/lib/Target/DirectX/DXILShaderFlags.h b/llvm/lib/Target/DirectX/DXILShaderFlags.h index e6c6d56402c1a..abf7cc86259ed 100644 --- a/llvm/lib/Target/DirectX/DXILShaderFlags.h +++ b/llvm/lib/Target/DirectX/DXILShaderFlags.h @@ -14,6 +14,7 @@ #ifndef LLVM_TARGET_DIRECTX_DXILSHADERFLAGS_H #define LLVM_TARGET_DIRECTX_DXILSHADERFLAGS_H +#include "llvm/Analysis/DXILMetadataAnalysis.h" #include "llvm/IR/Function.h" #include "llvm/IR/PassManager.h" #include "llvm/Pass.h" @@ -83,7 +84,8 @@ struct ComputedShaderFlags { }; struct ModuleShaderFlags { - void initialize(Module &, DXILResourceTypeMap &DRTM); + void initialize(Module &, DXILResourceTypeMap &DRTM, + const ModuleMetadataInfo &MMDI); const ComputedShaderFlags &getFunctionFlags(const Function *) const; const ComputedShaderFlags &getCombinedFlags() const { return CombinedSFMask; } diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-cs.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-cs.ll new file mode 100644 index 0000000000000..421c8b67350c2 --- /dev/null +++ b/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-cs.ll @@ -0,0 +1,34 @@ +; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s + + +; CHECK: ; Combined Shader Flags for Module +; CHECK-NEXT: ; Shader Flags Value: 0x00000001 + +; CHECK: ; Note: extra DXIL module flags: +; CHECK-NEXT: ; D3D11_1_SB_GLOBAL_FLAG_SKIP_OPTIMIZATION + +; CHECK: ; Shader Flags for Module Functions +; CHECK: ; Function main : 0x00000000 +; The test source in this file generated from the following command: +; clang -cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -emit-llvm -O0 -o - <&1 %s | FileCheck %s + + +; CHECK: ; Combined Shader Flags for Module +; CHECK-NEXT: ; Shader Flags Value: 0x00000001 + +; CHECK: ; Note: extra DXIL module flags: +; CHECK-NEXT: ; D3D11_1_SB_GLOBAL_FLAG_SKIP_OPTIMIZATION + +; CHECK: ; Shader Flags for Module Functions +; CHECK: ; Function main : 0x00000000 +; The test source in this file generated from the following command: +; clang -cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -emit-llvm -O0 -o - <&1 %s | FileCheck %s + +target triple = "dxilv1.3-pc-shadermodel6.3-library" + +; All entry functions of a library shader need to either have optnone +; or not have the attribute +; CHECK: error: +; CHECK-SAME: in function entry_two +; CHECK-SAME: Inconsistent optnone attribute +; Function Attrs: convergent noinline norecurse optnone +define void @entry_one() #0 { +entry: + ret void +} + +; Function Attrs: convergent noinline norecurse +define void @entry_two() #1 { +entry: + ret void +} + +attributes #0 = { convergent noinline norecurse optnone "approx-func-fp-math"="true" "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" } +attributes #1 = { convergent noinline norecurse "approx-func-fp-math"="true" "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" } + +!llvm.module.flags = !{!0, !1} +!dx.valver = !{!2} + +!0 = !{i32 1, !"wchar_size", i32 4} +!1 = !{i32 4, !"dx.disable_optimizations", i32 1} +!2 = !{i32 1, i32 8} diff --git a/llvm/test/CodeGen/DirectX/llc-pipeline.ll b/llvm/test/CodeGen/DirectX/llc-pipeline.ll index b071557249414..03b2150bbc1dc 100644 --- a/llvm/test/CodeGen/DirectX/llc-pipeline.ll +++ b/llvm/test/CodeGen/DirectX/llc-pipeline.ll @@ -23,8 +23,8 @@ ; CHECK-NEXT: Scalarize vector operations ; CHECK-NEXT: DXIL Resource Binding Analysis ; CHECK-NEXT: DXIL resource Information -; CHECK-NEXT: DXIL Shader Flag Analysis ; CHECK-NEXT: DXIL Module Metadata analysis +; CHECK-NEXT: DXIL Shader Flag Analysis ; CHECK-NEXT: DXIL Translate Metadata ; CHECK-NEXT: DXIL Op Lowering ; CHECK-NEXT: DXIL Prepare Module