Skip to content

[DirectX] Implement Shader Flags Analysis for ResMayNotAlias #131070

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 12 commits into from
Apr 10, 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
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/CodeGenOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,9 @@ CODEGENOPT(ImportCallOptimization, 1, 0)
/// (BlocksRuntime) on Windows.
CODEGENOPT(StaticClosure, 1, 0)

/// Assume that UAVs/SRVs may alias
CODEGENOPT(ResMayAlias, 1, 0)

/// FIXME: Make DebugOptions its own top-level .def file.
#include "DebugOptions.def"

Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -9131,6 +9131,11 @@ def dxil_validator_version : Option<["/", "-"], "validator-version", KIND_SEPARA
HelpText<"Override validator version for module. Format: <major.minor>;"
"Default: DXIL.dll version or current internal version">,
MarshallingInfoString<TargetOpts<"DxilValidatorVersion">, "\"1.8\"">;
def res_may_alias : Option<["/", "-"], "res-may-alias", KIND_FLAG>,
Group<dxc_Group>, Flags<[HelpHidden]>,
Visibility<[DXCOption, ClangOption, CC1Option]>,
HelpText<"Assume that UAVs/SRVs may alias">,
Copy link
Contributor Author

@Icohedron Icohedron Mar 14, 2025

Choose a reason for hiding this comment

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

This HelpText matches that found in DXC.
Doesn't the flag only affect whether or not the ResMayNotAlias flag can be set? The logic for setting the RestMayNotAlias flag only takes UAVs into consideration.
Perhaps the mention of SRVs should be removed?

MarshallingInfoFlag<CodeGenOpts<"ResMayAlias">>;
def target_profile : DXCJoinedOrSeparate<"T">, MetaVarName<"<profile>">,
HelpText<"Set target profile">,
Values<"ps_6_0, ps_6_1, ps_6_2, ps_6_3, ps_6_4, ps_6_5, ps_6_6, ps_6_7,"
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/CodeGen/CGHLSLRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,14 @@ void CGHLSLRuntime::addHLSLBufferLayoutType(const RecordType *StructType,

void CGHLSLRuntime::finishCodeGen() {
auto &TargetOpts = CGM.getTarget().getTargetOpts();
auto &CodeGenOpts = CGM.getCodeGenOpts();
auto &LangOpts = CGM.getLangOpts();
llvm::Module &M = CGM.getModule();
Triple T(M.getTargetTriple());
if (T.getArch() == Triple::ArchType::dxil)
addDxilValVersion(TargetOpts.DxilValidatorVersion, M);
if (CodeGenOpts.ResMayAlias)
M.setModuleFlag(llvm::Module::ModFlagBehavior::Error, "dx.resmayalias", 1);

// NativeHalfType corresponds to the -fnative-half-type clang option which is
// aliased by clang-dxc's -enable-16bit-types option. This option is used to
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3974,6 +3974,7 @@ static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs,
static void RenderHLSLOptions(const ArgList &Args, ArgStringList &CmdArgs,
types::ID InputType) {
const unsigned ForwardedArguments[] = {options::OPT_dxil_validator_version,
options::OPT_res_may_alias,
options::OPT_D,
options::OPT_I,
options::OPT_O,
Expand Down
7 changes: 7 additions & 0 deletions clang/test/CodeGenHLSL/res-may-alias.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %clang_dxc -res-may-alias -T lib_6_3 -HV 202x -Vd -Xclang -emit-llvm %s | FileCheck %s --check-prefix=FLAG
// RUN: %clang_dxc -T lib_6_3 -HV 202x -Vd -Xclang -emit-llvm %s | FileCheck %s --check-prefix=NOFLAG

// FLAG-DAG: ![[RMA:.*]] = !{i32 1, !"dx.resmayalias", i32 1}
// FLAG-DAG: !llvm.module.flags = !{{{.*}}![[RMA]]{{.*}}}

// NOFLAG-NOT: dx.resmayalias
40 changes: 34 additions & 6 deletions llvm/lib/Target/DirectX/DXILShaderFlags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ static bool checkWaveOps(Intrinsic::ID IID) {
/// \param I Instruction to check.
void ModuleShaderFlags::updateFunctionFlags(ComputedShaderFlags &CSF,
const Instruction &I,
DXILResourceTypeMap &DRTM) {
DXILResourceTypeMap &DRTM,
const ModuleMetadataInfo &MMDI) {
if (!CSF.Doubles)
CSF.Doubles = I.getType()->isDoubleTy();

Expand Down Expand Up @@ -128,8 +129,15 @@ void ModuleShaderFlags::updateFunctionFlags(ComputedShaderFlags &CSF,
switch (II->getIntrinsicID()) {
default:
break;
case Intrinsic::dx_resource_handlefrombinding:
switch (DRTM[cast<TargetExtType>(II->getType())].getResourceKind()) {
case Intrinsic::dx_resource_handlefrombinding: {
dxil::ResourceTypeInfo &RTI = DRTM[cast<TargetExtType>(II->getType())];

// Set ResMayNotAlias if DXIL version >= 1.8 and function uses UAVs
if (!CSF.ResMayNotAlias && CanSetResMayNotAlias &&
MMDI.DXILVersion >= VersionTuple(1, 8) && RTI.isUAV())
CSF.ResMayNotAlias = true;

switch (RTI.getResourceKind()) {
case dxil::ResourceKind::StructuredBuffer:
case dxil::ResourceKind::RawBuffer:
CSF.EnableRawAndStructuredBuffers = true;
Expand All @@ -138,6 +146,7 @@ void ModuleShaderFlags::updateFunctionFlags(ComputedShaderFlags &CSF,
break;
}
break;
}
case Intrinsic::dx_resource_load_typedbuffer: {
dxil::ResourceTypeInfo &RTI =
DRTM[cast<TargetExtType>(II->getArgOperand(0)->getType())];
Expand All @@ -163,7 +172,18 @@ void ModuleShaderFlags::updateFunctionFlags(ComputedShaderFlags &CSF,

/// Construct ModuleShaderFlags for module Module M
void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
DXILResourceMap &DRM,
const ModuleMetadataInfo &MMDI) {

CanSetResMayNotAlias = MMDI.DXILVersion >= VersionTuple(1, 7);

// Check if -res-may-alias was provided on the command line.
// The command line option will set the dx.resmayalias module flag to 1.
if (auto *RMA = mdconst::extract_or_null<ConstantInt>(
M.getModuleFlag("dx.resmayalias")))
if (RMA->getValue() != 0)
CanSetResMayNotAlias = false;

CallGraph CG(M);

// Compute Shader Flags Mask for all functions using post-order visit of SCC
Expand All @@ -188,6 +208,11 @@ void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
continue;
}

// Set ResMayNotAlias to true if DXIL version < 1.8 and there are UAVs
// present globally.
if (CanSetResMayNotAlias && MMDI.DXILVersion < VersionTuple(1, 8))
SCCSF.ResMayNotAlias = !DRM.uavs().empty();

// Set UseNativeLowPrecision using dx.nativelowprec module metadata
if (auto *NativeLowPrec = mdconst::extract_or_null<ConstantInt>(
M.getModuleFlag("dx.nativelowprec")))
Expand All @@ -198,7 +223,7 @@ void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
ComputedShaderFlags CSF;
for (const auto &BB : *F)
for (const auto &I : BB)
updateFunctionFlags(CSF, I, DRTM);
updateFunctionFlags(CSF, I, DRTM, MMDI);
// Update combined shader flags mask for all functions in this SCC
SCCSF.merge(CSF);

Expand Down Expand Up @@ -268,10 +293,11 @@ AnalysisKey ShaderFlagsAnalysis::Key;
ModuleShaderFlags ShaderFlagsAnalysis::run(Module &M,
ModuleAnalysisManager &AM) {
DXILResourceTypeMap &DRTM = AM.getResult<DXILResourceTypeAnalysis>(M);
DXILResourceMap &DRM = AM.getResult<DXILResourceAnalysis>(M);
const ModuleMetadataInfo MMDI = AM.getResult<DXILMetadataAnalysis>(M);

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

return MSFI;
}
Expand Down Expand Up @@ -301,16 +327,18 @@ PreservedAnalyses ShaderFlagsAnalysisPrinter::run(Module &M,
bool ShaderFlagsAnalysisWrapper::runOnModule(Module &M) {
DXILResourceTypeMap &DRTM =
getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
DXILResourceMap &DRM = getAnalysis<DXILResourceWrapperPass>().getBindingMap();
const ModuleMetadataInfo MMDI =
getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();

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

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

Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Target/DirectX/DXILShaderFlags.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace llvm {
class Module;
class GlobalVariable;
class DXILResourceTypeMap;
class DXILResourceMap;

namespace dxil {

Expand Down Expand Up @@ -84,20 +85,21 @@ struct ComputedShaderFlags {
};

struct ModuleShaderFlags {
void initialize(Module &, DXILResourceTypeMap &DRTM,
void initialize(Module &, DXILResourceTypeMap &DRTM, DXILResourceMap &DRM,
const ModuleMetadataInfo &MMDI);
const ComputedShaderFlags &getFunctionFlags(const Function *) const;
const ComputedShaderFlags &getCombinedFlags() const { return CombinedSFMask; }

private:
bool CanSetResMayNotAlias;
/// Map of Function-Shader Flag Mask pairs representing properties of each of
/// the functions in the module. Shader Flags of each function represent both
/// module-level and function-level flags
DenseMap<const Function *, ComputedShaderFlags> FunctionFlags;
/// Combined Shader Flag Mask of all functions of the module
ComputedShaderFlags CombinedSFMask{};
void updateFunctionFlags(ComputedShaderFlags &, const Instruction &,
DXILResourceTypeMap &);
DXILResourceTypeMap &, const ModuleMetadataInfo &);
};

class ShaderFlagsAnalysis : public AnalysisInfoMixin<ShaderFlagsAnalysis> {
Expand Down
43 changes: 43 additions & 0 deletions llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-0.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s

; This test checks to ensure that setting the LLVM module flag "dx.resmayalias"
; to 0 has no effect on the DXIL shader flag analysis for the flag
; ResMayNotAlias.

target triple = "dxil-pc-shadermodel6.8-library"

; CHECK: Combined Shader Flags for Module
; CHECK-NEXT: Shader Flags Value: 0x200000010
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems the ResMayNotAlias flag is what we are checking and is the 2 value. What is the 1 value for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The 1 is for the structured buffer (SRV) in the same module. It's there so I can test that the function that accesses only an SRV will not get the ResMayNotAlias flag.


; CHECK: Note: extra DXIL module flags:
; CHECK: Raw and Structured buffers
; CHECK: Any UAV may not alias any other UAV
;

; CHECK: Function loadUAV : 0x20000000
define float @loadUAV() #0 {
%res = call target("dx.TypedBuffer", float, 1, 0, 0)
@llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false)
%load = call {float, i1} @llvm.dx.resource.load.typedbuffer(
target("dx.TypedBuffer", float, 1, 0, 0) %res, i32 0)
%val = extractvalue {float, i1} %load, 0
ret float %val
}

; CHECK: Function loadSRV : 0x00000010
define float @loadSRV() #0 {
%res = tail call target("dx.RawBuffer", float, 0, 0)
@llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false)
%load = call {float, i1} @llvm.dx.resource.load.rawbuffer(
target("dx.RawBuffer", float, 0, 0) %res, i32 0, i32 0)
%val = extractvalue { float, i1 } %load, 0
ret float %val
}

!llvm.module.flags = !{!0}

; dx.resmayalias should never appear with a value of 0.
; But if it does, ensure that it has no effect.
!0 = !{i32 1, !"dx.resmayalias", i32 0}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if this test is necessary since the -res-may-alias clang-dxc flag will set this to 1. If the clang-dxc flag is not present, then this metadata would not appear at all. Let me know if I should delete this test.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the moment we define some metadata we should expect that some user may set the field manually, so having tests for this should definitely be included


attributes #0 = { convergent norecurse nounwind "hlsl.export"}
41 changes: 41 additions & 0 deletions llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-1.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s

; This test checks to ensure that setting the LLVM module flag "dx.resmayalias"
; to 1 prevents the DXIL shader flag analysis from setting the flag
; ResMayNotAlias.

target triple = "dxil-pc-shadermodel6.8-library"

; CHECK: Combined Shader Flags for Module
; CHECK-NEXT: Shader Flags Value: 0x00000010

; CHECK: Note: extra DXIL module flags:
; CHECK: Raw and Structured buffers
; CHECK-NOT: Any UAV may not alias any other UAV
;

; CHECK: Function loadUAV : 0x00000000
define float @loadUAV() #0 {
%res = call target("dx.TypedBuffer", float, 1, 0, 0)
@llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false)
%load = call {float, i1} @llvm.dx.resource.load.typedbuffer(
target("dx.TypedBuffer", float, 1, 0, 0) %res, i32 0)
%val = extractvalue {float, i1} %load, 0
ret float %val
}

; CHECK: Function loadSRV : 0x00000010
define float @loadSRV() #0 {
%res = tail call target("dx.RawBuffer", float, 0, 0)
@llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false)
%load = call {float, i1} @llvm.dx.resource.load.rawbuffer(
target("dx.RawBuffer", float, 0, 0) %res, i32 0, i32 0)
%val = extractvalue { float, i1 } %load, 0
ret float %val
}

!llvm.module.flags = !{!0}

!0 = !{i32 1, !"dx.resmayalias", i32 1}

attributes #0 = { convergent norecurse nounwind "hlsl.export"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s

; This test checks to ensure the behavior of the DXIL shader flag analysis
; for the flag ResMayNotAlias is correct when the DXIL Version is 1.6. The
; ResMayNotAlias flag (0x20000000) should not be set at all.

target triple = "dxil-pc-shadermodel6.6-library"

; CHECK: Combined Shader Flags for Module
; CHECK-NEXT: Shader Flags Value: 0x00000010

; CHECK: Note: extra DXIL module flags:
; CHECK: Raw and Structured buffers
; CHECK-NOT: Any UAV may not alias any other UAV
;

; CHECK: Function loadUAV : 0x00000000
define float @loadUAV() #0 {
%res = call target("dx.TypedBuffer", float, 1, 0, 0)
@llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false)
%load = call {float, i1} @llvm.dx.resource.load.typedbuffer(
target("dx.TypedBuffer", float, 1, 0, 0) %res, i32 0)
%val = extractvalue {float, i1} %load, 0
ret float %val
}

; CHECK: Function loadSRV : 0x00000010
define float @loadSRV() #0 {
%res = tail call target("dx.RawBuffer", float, 0, 0)
@llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false)
%load = call {float, i1} @llvm.dx.resource.load.rawbuffer(
target("dx.RawBuffer", float, 0, 0) %res, i32 0, i32 0)
%val = extractvalue { float, i1 } %load, 0
ret float %val
}

attributes #0 = { convergent norecurse nounwind "hlsl.export"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s

; This test checks to ensure the behavior of the DXIL shader flag analysis
; for the flag ResMayNotAlias is correct when the DXIL Version is 1.7. The
; ResMayNotAlias flag (0x20000000) should be set on all functions if there are
; one or more UAVs present globally in the module.

target triple = "dxil-pc-shadermodel6.7-library"

; CHECK: Combined Shader Flags for Module
; CHECK-NEXT: Shader Flags Value: 0x200000010

; CHECK: Note: extra DXIL module flags:
; CHECK: Raw and Structured buffers
; CHECK: Any UAV may not alias any other UAV
;

; CHECK: Function loadUAV : 0x200000000
define float @loadUAV() #0 {
%res = call target("dx.TypedBuffer", float, 1, 0, 0)
@llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false)
%load = call {float, i1} @llvm.dx.resource.load.typedbuffer(
target("dx.TypedBuffer", float, 1, 0, 0) %res, i32 0)
%val = extractvalue {float, i1} %load, 0
ret float %val
}

; CHECK: Function loadSRV : 0x200000010
define float @loadSRV() #0 {
%res = tail call target("dx.RawBuffer", float, 0, 0)
@llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false)
%load = call {float, i1} @llvm.dx.resource.load.rawbuffer(
target("dx.RawBuffer", float, 0, 0) %res, i32 0, i32 0)
%val = extractvalue { float, i1 } %load, 0
ret float %val
}

attributes #0 = { convergent norecurse nounwind "hlsl.export"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s

; This test checks to ensure the behavior of the DXIL shader flag analysis
; for the flag ResMayNotAlias is correct when the DXIL Version is 1.8. The
; ResMayNotAlias flag (0x20000000) should only be set when a function uses a
; UAV.

target triple = "dxil-pc-shadermodel6.8-library"

; CHECK: Combined Shader Flags for Module
; CHECK-NEXT: Shader Flags Value: 0x200000010

; CHECK: Note: extra DXIL module flags:
; CHECK: Raw and Structured buffers
; CHECK: Any UAV may not alias any other UAV
;

; CHECK: Function loadUAV : 0x20000000
define float @loadUAV() #0 {
%res = call target("dx.TypedBuffer", float, 1, 0, 0)
@llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false)
%load = call {float, i1} @llvm.dx.resource.load.typedbuffer(
target("dx.TypedBuffer", float, 1, 0, 0) %res, i32 0)
%val = extractvalue {float, i1} %load, 0
ret float %val
}

; CHECK: Function loadSRV : 0x00000010
define float @loadSRV() #0 {
%res = tail call target("dx.RawBuffer", float, 0, 0)
@llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false)
%load = call {float, i1} @llvm.dx.resource.load.rawbuffer(
target("dx.RawBuffer", float, 0, 0) %res, i32 0, i32 0)
%val = extractvalue { float, i1 } %load, 0
ret float %val
}

attributes #0 = { convergent norecurse nounwind "hlsl.export"}
Loading
Loading