Skip to content

[DirectX] Set Shader Flag DisableOptimizations #126813

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

Merged
merged 2 commits into from
Feb 12, 2025
Merged
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
6 changes: 0 additions & 6 deletions clang/lib/CodeGen/CGHLSLRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -171,8 +167,6 @@ void CGHLSLRuntime::finishCodeGen() {
addDxilValVersion(TargetOpts.DxilValidatorVersion, M);

generateGlobalCtorDtorCalls();
if (CGM.getCodeGenOpts().OptimizationLevel == 0)
addDisableOptimizations(M);

const DataLayout &DL = M.getDataLayout();

Expand Down
12 changes: 0 additions & 12 deletions clang/test/CodeGenHLSL/disable_opt.hlsl

This file was deleted.

28 changes: 25 additions & 3 deletions llvm/lib/Target/DirectX/DXILShaderFlags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -180,9 +197,10 @@ AnalysisKey ShaderFlagsAnalysis::Key;
ModuleShaderFlags ShaderFlagsAnalysis::run(Module &M,
ModuleAnalysisManager &AM) {
DXILResourceTypeMap &DRTM = AM.getResult<DXILResourceTypeAnalysis>(M);
const ModuleMetadataInfo MMDI = AM.getResult<DXILMetadataAnalysis>(M);

ModuleShaderFlags MSFI;
MSFI.initialize(M, DRTM);
MSFI.initialize(M, DRTM, MMDI);

return MSFI;
}
Expand Down Expand Up @@ -212,20 +230,24 @@ PreservedAnalyses ShaderFlagsAnalysisPrinter::run(Module &M,
bool ShaderFlagsAnalysisWrapper::runOnModule(Module &M) {
DXILResourceTypeMap &DRTM =
getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
const ModuleMetadataInfo MMDI =
getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();

MSFI.initialize(M, DRTM);
MSFI.initialize(M, DRTM, MMDI);
return false;
}

void ShaderFlagsAnalysisWrapper::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequiredTransitive<DXILResourceTypeWrapperPass>();
AU.addRequired<DXILMetadataAnalysisWrapperPass>();
Copy link
Member

@farzonl farzonl Feb 12, 2025

Choose a reason for hiding this comment

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

This change causes DXIL Module Metadata analysis pass to run before DXIL Shader Flag Analysis because it is now needed MMDI.EntryPropertyVec[0].Entry->hasFnAttribute( llvm::Attribute::OptimizeNone); to initialize ModuleShaderFlags.

I think before we never intended to fetch fn attributes since you were using dx.disable_optimizations.
Does that make optnone a one off here or are there other attributes that could use this pattern?

Copy link
Contributor Author

@bharadwajy bharadwajy Feb 12, 2025

Choose a reason for hiding this comment

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

This change causes DXIL Module Metadata analysis pass to run before DXIL Shader Flag Analysis because it is now needed MMDI.EntryPropertyVec[0].Entry->hasFnAttribute( llvm::Attribute::OptimizeNone); to initialize ModuleShaderFlags.

I think before we never intended to fetch fn attributes since you were using dx.disable_optimizations.

Yes, that was the proposed mechanism in PR #123136. This PR supersedes that following the feedback there.

Does that make optnone a one off here or are there other attributes that could use this pattern?

The list of entry function information collected by Metadata Analysis pass is leveraged in this pass to look at optnone of each of the entry functions. Similarly it can be leveraged by other passes that require access to entry function information - for example, a pass that would want to query for entry function declaration withHLSLWaveSizeAttr.

Entry functions can be collected during the call graph traversal in this pass to query for optnone attribute. I just chose to leverage the same info that would be built in Metadata Analysis pass.

}

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)
4 changes: 3 additions & 1 deletion llvm/lib/Target/DirectX/DXILShaderFlags.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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; }

Expand Down
34 changes: 34 additions & 0 deletions llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-cs.ll
Original file line number Diff line number Diff line change
@@ -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 - <<EOF
; [numthreads(1,1,1)]
; [shader("compute")]
; void main() {}
; EOF

target triple = "dxilv1.0-pc-shadermodel6.0-compute"

; Function Attrs: convergent noinline norecurse optnone
define void @main() #0 {
entry:
ret void
}

; Function Attrs: alwaysinline convergent mustprogress norecurse nounwind
define noundef i32 @_Z3foov() #1 {
entry:
ret i32 0
}

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 = { alwaysinline convergent mustprogress norecurse nounwind "approx-func-fp-math"="true" "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" }
44 changes: 44 additions & 0 deletions llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-lib.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
; 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.3-library -x hlsl -emit-llvm -O0 -o - <<EOF

; [numthreads(1,1,1)]
; [shader("compute")]
; void main() {}

; int foo() {return 0;}
; EOF

target triple = "dxilv1.3-pc-shadermodel6.3-library"

; Function Attrs: convergent mustprogress noinline norecurse nounwind optnone
define internal void @_Z4mainv() #0 {
entry:
ret void
}

; Function Attrs: convergent noinline norecurse optnone
define void @main() #1 {
entry:
call void @_Z4mainv()
ret void
}

; Function Attrs: convergent mustprogress noinline norecurse nounwind optnone
define noundef i32 @_Z3foov() #0 {
entry:
ret i32 0
}

attributes #0 = { convergent mustprogress noinline norecurse nounwind optnone "approx-func-fp-math"="true" "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 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" }
30 changes: 30 additions & 0 deletions llvm/test/CodeGen/DirectX/ShaderFlags/lib-entry-attr-error.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
; RUN: not opt -S --passes="print-dx-shader-flags" 2>&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}
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/DirectX/llc-pipeline.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down