[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-04-10 Thread Deric C. via cfe-commits

https://github.com/Icohedron closed 
https://github.com/llvm/llvm-project/pull/131070
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-04-10 Thread Farzon Lotfi via cfe-commits

https://github.com/farzonl approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/131070
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-04-10 Thread Deric C. via cfe-commits

https://github.com/Icohedron updated 
https://github.com/llvm/llvm-project/pull/131070

>From 81196e016dbf1209637dd13315efff7eac461d42 Mon Sep 17 00:00:00 2001
From: Icohedron 
Date: Fri, 14 Mar 2025 00:24:26 +
Subject: [PATCH 01/10] Implement ResMayNotAlias DXIL shader flag analysis

---
 clang/include/clang/Basic/CodeGenOptions.def  |  3 ++
 clang/include/clang/Driver/Options.td |  5 +++
 clang/lib/CodeGen/CGHLSLRuntime.cpp   |  3 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  1 +
 clang/test/CodeGenHLSL/res-may-alias.hlsl |  7 +++
 llvm/lib/Target/DirectX/DXILShaderFlags.cpp   | 43 ---
 llvm/lib/Target/DirectX/DXILShaderFlags.h |  9 +++-
 .../DirectX/ShaderFlags/res-may-alias-0.ll| 39 +
 .../DirectX/ShaderFlags/res-may-alias-1.ll| 37 
 .../res-may-not-alias-shadermodel6.7.ll   | 33 ++
 .../res-may-not-alias-shadermodel6.8.ll   | 33 ++
 .../typed-uav-load-additional-formats.ll  |  9 ++--
 12 files changed, 210 insertions(+), 12 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/res-may-alias.hlsl
 create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-0.ll
 create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-1.ll
 create mode 100644 
llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-shadermodel6.7.ll
 create mode 100644 
llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-shadermodel6.8.ll

diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index a7f5f1abbb825..a436c0ec98d5b 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -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"
 
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 66ae8f1c7f064..9c5fd2354f95e 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -9043,6 +9043,11 @@ def dxil_validator_version : Option<["/", "-"], 
"validator-version", KIND_SEPARA
   HelpText<"Override validator version for module. Format: ;"
"Default: DXIL.dll version or current internal version">,
   MarshallingInfoString, "\"1.8\"">;
+def res_may_alias : Option<["/", "-"], "res-may-alias", KIND_FLAG>,
+  Group, Flags<[HelpHidden]>,
+  Visibility<[DXCOption, ClangOption, CC1Option]>,
+  HelpText<"Assume that UAVs/SRVs may alias">,
+  MarshallingInfoFlag>;
 def target_profile : DXCJoinedOrSeparate<"T">, MetaVarName<"">,
   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,"
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp 
b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 5916fa6183a27..6fd8bc295e8ca 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -283,10 +283,13 @@ void CGHLSLRuntime::addHLSLBufferLayoutType(const 
RecordType *StructType,
 
 void CGHLSLRuntime::finishCodeGen() {
   auto &TargetOpts = CGM.getTarget().getTargetOpts();
+  auto &CodeGenOpts = CGM.getCodeGenOpts();
   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);
 
   generateGlobalCtorDtorCalls();
 }
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index fe172d923ac07..9be3939641cfc 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -3959,6 +3959,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,
diff --git a/clang/test/CodeGenHLSL/res-may-alias.hlsl 
b/clang/test/CodeGenHLSL/res-may-alias.hlsl
new file mode 100644
index 0..53ee8ee4935d8
--- /dev/null
+++ b/clang/test/CodeGenHLSL/res-may-alias.hlsl
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -res-may-alias -finclude-default-header -triple 
dxil-pc-shadermodel6.3-library -emit-llvm -disable-llvm-passes -o - %s | 
FileCheck %s --check-prefix=FLAG
+// RUN: %clang_cc1 -finclude-default-header -triple 
dxil-pc-shadermodel6.3-library -emit-llvm -disabl

[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-04-09 Thread Deric C. via cfe-commits

https://github.com/Icohedron updated 
https://github.com/llvm/llvm-project/pull/131070

>From 81196e016dbf1209637dd13315efff7eac461d42 Mon Sep 17 00:00:00 2001
From: Icohedron 
Date: Fri, 14 Mar 2025 00:24:26 +
Subject: [PATCH 01/10] Implement ResMayNotAlias DXIL shader flag analysis

---
 clang/include/clang/Basic/CodeGenOptions.def  |  3 ++
 clang/include/clang/Driver/Options.td |  5 +++
 clang/lib/CodeGen/CGHLSLRuntime.cpp   |  3 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  1 +
 clang/test/CodeGenHLSL/res-may-alias.hlsl |  7 +++
 llvm/lib/Target/DirectX/DXILShaderFlags.cpp   | 43 ---
 llvm/lib/Target/DirectX/DXILShaderFlags.h |  9 +++-
 .../DirectX/ShaderFlags/res-may-alias-0.ll| 39 +
 .../DirectX/ShaderFlags/res-may-alias-1.ll| 37 
 .../res-may-not-alias-shadermodel6.7.ll   | 33 ++
 .../res-may-not-alias-shadermodel6.8.ll   | 33 ++
 .../typed-uav-load-additional-formats.ll  |  9 ++--
 12 files changed, 210 insertions(+), 12 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/res-may-alias.hlsl
 create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-0.ll
 create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-1.ll
 create mode 100644 
llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-shadermodel6.7.ll
 create mode 100644 
llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-shadermodel6.8.ll

diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index a7f5f1abbb825..a436c0ec98d5b 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -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"
 
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 66ae8f1c7f064..9c5fd2354f95e 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -9043,6 +9043,11 @@ def dxil_validator_version : Option<["/", "-"], 
"validator-version", KIND_SEPARA
   HelpText<"Override validator version for module. Format: ;"
"Default: DXIL.dll version or current internal version">,
   MarshallingInfoString, "\"1.8\"">;
+def res_may_alias : Option<["/", "-"], "res-may-alias", KIND_FLAG>,
+  Group, Flags<[HelpHidden]>,
+  Visibility<[DXCOption, ClangOption, CC1Option]>,
+  HelpText<"Assume that UAVs/SRVs may alias">,
+  MarshallingInfoFlag>;
 def target_profile : DXCJoinedOrSeparate<"T">, MetaVarName<"">,
   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,"
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp 
b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 5916fa6183a27..6fd8bc295e8ca 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -283,10 +283,13 @@ void CGHLSLRuntime::addHLSLBufferLayoutType(const 
RecordType *StructType,
 
 void CGHLSLRuntime::finishCodeGen() {
   auto &TargetOpts = CGM.getTarget().getTargetOpts();
+  auto &CodeGenOpts = CGM.getCodeGenOpts();
   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);
 
   generateGlobalCtorDtorCalls();
 }
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index fe172d923ac07..9be3939641cfc 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -3959,6 +3959,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,
diff --git a/clang/test/CodeGenHLSL/res-may-alias.hlsl 
b/clang/test/CodeGenHLSL/res-may-alias.hlsl
new file mode 100644
index 0..53ee8ee4935d8
--- /dev/null
+++ b/clang/test/CodeGenHLSL/res-may-alias.hlsl
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -res-may-alias -finclude-default-header -triple 
dxil-pc-shadermodel6.3-library -emit-llvm -disable-llvm-passes -o - %s | 
FileCheck %s --check-prefix=FLAG
+// RUN: %clang_cc1 -finclude-default-header -triple 
dxil-pc-shadermodel6.3-library -emit-llvm -disabl

[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-04-09 Thread Deric C. via cfe-commits

https://github.com/Icohedron updated 
https://github.com/llvm/llvm-project/pull/131070

>From 81196e016dbf1209637dd13315efff7eac461d42 Mon Sep 17 00:00:00 2001
From: Icohedron 
Date: Fri, 14 Mar 2025 00:24:26 +
Subject: [PATCH 1/9] Implement ResMayNotAlias DXIL shader flag analysis

---
 clang/include/clang/Basic/CodeGenOptions.def  |  3 ++
 clang/include/clang/Driver/Options.td |  5 +++
 clang/lib/CodeGen/CGHLSLRuntime.cpp   |  3 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  1 +
 clang/test/CodeGenHLSL/res-may-alias.hlsl |  7 +++
 llvm/lib/Target/DirectX/DXILShaderFlags.cpp   | 43 ---
 llvm/lib/Target/DirectX/DXILShaderFlags.h |  9 +++-
 .../DirectX/ShaderFlags/res-may-alias-0.ll| 39 +
 .../DirectX/ShaderFlags/res-may-alias-1.ll| 37 
 .../res-may-not-alias-shadermodel6.7.ll   | 33 ++
 .../res-may-not-alias-shadermodel6.8.ll   | 33 ++
 .../typed-uav-load-additional-formats.ll  |  9 ++--
 12 files changed, 210 insertions(+), 12 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/res-may-alias.hlsl
 create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-0.ll
 create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-1.ll
 create mode 100644 
llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-shadermodel6.7.ll
 create mode 100644 
llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-shadermodel6.8.ll

diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index a7f5f1abbb825..a436c0ec98d5b 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -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"
 
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 66ae8f1c7f064..9c5fd2354f95e 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -9043,6 +9043,11 @@ def dxil_validator_version : Option<["/", "-"], 
"validator-version", KIND_SEPARA
   HelpText<"Override validator version for module. Format: ;"
"Default: DXIL.dll version or current internal version">,
   MarshallingInfoString, "\"1.8\"">;
+def res_may_alias : Option<["/", "-"], "res-may-alias", KIND_FLAG>,
+  Group, Flags<[HelpHidden]>,
+  Visibility<[DXCOption, ClangOption, CC1Option]>,
+  HelpText<"Assume that UAVs/SRVs may alias">,
+  MarshallingInfoFlag>;
 def target_profile : DXCJoinedOrSeparate<"T">, MetaVarName<"">,
   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,"
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp 
b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 5916fa6183a27..6fd8bc295e8ca 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -283,10 +283,13 @@ void CGHLSLRuntime::addHLSLBufferLayoutType(const 
RecordType *StructType,
 
 void CGHLSLRuntime::finishCodeGen() {
   auto &TargetOpts = CGM.getTarget().getTargetOpts();
+  auto &CodeGenOpts = CGM.getCodeGenOpts();
   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);
 
   generateGlobalCtorDtorCalls();
 }
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index fe172d923ac07..9be3939641cfc 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -3959,6 +3959,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,
diff --git a/clang/test/CodeGenHLSL/res-may-alias.hlsl 
b/clang/test/CodeGenHLSL/res-may-alias.hlsl
new file mode 100644
index 0..53ee8ee4935d8
--- /dev/null
+++ b/clang/test/CodeGenHLSL/res-may-alias.hlsl
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -res-may-alias -finclude-default-header -triple 
dxil-pc-shadermodel6.3-library -emit-llvm -disable-llvm-passes -o - %s | 
FileCheck %s --check-prefix=FLAG
+// RUN: %clang_cc1 -finclude-default-header -triple 
dxil-pc-shadermodel6.3-library -emit-llvm -disable-

[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-04-09 Thread Deric C. via cfe-commits

https://github.com/Icohedron updated 
https://github.com/llvm/llvm-project/pull/131070

>From 81196e016dbf1209637dd13315efff7eac461d42 Mon Sep 17 00:00:00 2001
From: Icohedron 
Date: Fri, 14 Mar 2025 00:24:26 +
Subject: [PATCH 1/8] Implement ResMayNotAlias DXIL shader flag analysis

---
 clang/include/clang/Basic/CodeGenOptions.def  |  3 ++
 clang/include/clang/Driver/Options.td |  5 +++
 clang/lib/CodeGen/CGHLSLRuntime.cpp   |  3 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  1 +
 clang/test/CodeGenHLSL/res-may-alias.hlsl |  7 +++
 llvm/lib/Target/DirectX/DXILShaderFlags.cpp   | 43 ---
 llvm/lib/Target/DirectX/DXILShaderFlags.h |  9 +++-
 .../DirectX/ShaderFlags/res-may-alias-0.ll| 39 +
 .../DirectX/ShaderFlags/res-may-alias-1.ll| 37 
 .../res-may-not-alias-shadermodel6.7.ll   | 33 ++
 .../res-may-not-alias-shadermodel6.8.ll   | 33 ++
 .../typed-uav-load-additional-formats.ll  |  9 ++--
 12 files changed, 210 insertions(+), 12 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/res-may-alias.hlsl
 create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-0.ll
 create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-1.ll
 create mode 100644 
llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-shadermodel6.7.ll
 create mode 100644 
llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-shadermodel6.8.ll

diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index a7f5f1abbb825..a436c0ec98d5b 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -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"
 
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 66ae8f1c7f064..9c5fd2354f95e 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -9043,6 +9043,11 @@ def dxil_validator_version : Option<["/", "-"], 
"validator-version", KIND_SEPARA
   HelpText<"Override validator version for module. Format: ;"
"Default: DXIL.dll version or current internal version">,
   MarshallingInfoString, "\"1.8\"">;
+def res_may_alias : Option<["/", "-"], "res-may-alias", KIND_FLAG>,
+  Group, Flags<[HelpHidden]>,
+  Visibility<[DXCOption, ClangOption, CC1Option]>,
+  HelpText<"Assume that UAVs/SRVs may alias">,
+  MarshallingInfoFlag>;
 def target_profile : DXCJoinedOrSeparate<"T">, MetaVarName<"">,
   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,"
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp 
b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 5916fa6183a27..6fd8bc295e8ca 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -283,10 +283,13 @@ void CGHLSLRuntime::addHLSLBufferLayoutType(const 
RecordType *StructType,
 
 void CGHLSLRuntime::finishCodeGen() {
   auto &TargetOpts = CGM.getTarget().getTargetOpts();
+  auto &CodeGenOpts = CGM.getCodeGenOpts();
   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);
 
   generateGlobalCtorDtorCalls();
 }
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index fe172d923ac07..9be3939641cfc 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -3959,6 +3959,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,
diff --git a/clang/test/CodeGenHLSL/res-may-alias.hlsl 
b/clang/test/CodeGenHLSL/res-may-alias.hlsl
new file mode 100644
index 0..53ee8ee4935d8
--- /dev/null
+++ b/clang/test/CodeGenHLSL/res-may-alias.hlsl
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -res-may-alias -finclude-default-header -triple 
dxil-pc-shadermodel6.3-library -emit-llvm -disable-llvm-passes -o - %s | 
FileCheck %s --check-prefix=FLAG
+// RUN: %clang_cc1 -finclude-default-header -triple 
dxil-pc-shadermodel6.3-library -emit-llvm -disable-

[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-04-09 Thread Deric C. via cfe-commits

https://github.com/Icohedron updated 
https://github.com/llvm/llvm-project/pull/131070

>From 81196e016dbf1209637dd13315efff7eac461d42 Mon Sep 17 00:00:00 2001
From: Icohedron 
Date: Fri, 14 Mar 2025 00:24:26 +
Subject: [PATCH 1/7] Implement ResMayNotAlias DXIL shader flag analysis

---
 clang/include/clang/Basic/CodeGenOptions.def  |  3 ++
 clang/include/clang/Driver/Options.td |  5 +++
 clang/lib/CodeGen/CGHLSLRuntime.cpp   |  3 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  1 +
 clang/test/CodeGenHLSL/res-may-alias.hlsl |  7 +++
 llvm/lib/Target/DirectX/DXILShaderFlags.cpp   | 43 ---
 llvm/lib/Target/DirectX/DXILShaderFlags.h |  9 +++-
 .../DirectX/ShaderFlags/res-may-alias-0.ll| 39 +
 .../DirectX/ShaderFlags/res-may-alias-1.ll| 37 
 .../res-may-not-alias-shadermodel6.7.ll   | 33 ++
 .../res-may-not-alias-shadermodel6.8.ll   | 33 ++
 .../typed-uav-load-additional-formats.ll  |  9 ++--
 12 files changed, 210 insertions(+), 12 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/res-may-alias.hlsl
 create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-0.ll
 create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-1.ll
 create mode 100644 
llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-shadermodel6.7.ll
 create mode 100644 
llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-shadermodel6.8.ll

diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index a7f5f1abbb825..a436c0ec98d5b 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -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"
 
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 66ae8f1c7f064..9c5fd2354f95e 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -9043,6 +9043,11 @@ def dxil_validator_version : Option<["/", "-"], 
"validator-version", KIND_SEPARA
   HelpText<"Override validator version for module. Format: ;"
"Default: DXIL.dll version or current internal version">,
   MarshallingInfoString, "\"1.8\"">;
+def res_may_alias : Option<["/", "-"], "res-may-alias", KIND_FLAG>,
+  Group, Flags<[HelpHidden]>,
+  Visibility<[DXCOption, ClangOption, CC1Option]>,
+  HelpText<"Assume that UAVs/SRVs may alias">,
+  MarshallingInfoFlag>;
 def target_profile : DXCJoinedOrSeparate<"T">, MetaVarName<"">,
   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,"
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp 
b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 5916fa6183a27..6fd8bc295e8ca 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -283,10 +283,13 @@ void CGHLSLRuntime::addHLSLBufferLayoutType(const 
RecordType *StructType,
 
 void CGHLSLRuntime::finishCodeGen() {
   auto &TargetOpts = CGM.getTarget().getTargetOpts();
+  auto &CodeGenOpts = CGM.getCodeGenOpts();
   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);
 
   generateGlobalCtorDtorCalls();
 }
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index fe172d923ac07..9be3939641cfc 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -3959,6 +3959,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,
diff --git a/clang/test/CodeGenHLSL/res-may-alias.hlsl 
b/clang/test/CodeGenHLSL/res-may-alias.hlsl
new file mode 100644
index 0..53ee8ee4935d8
--- /dev/null
+++ b/clang/test/CodeGenHLSL/res-may-alias.hlsl
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -res-may-alias -finclude-default-header -triple 
dxil-pc-shadermodel6.3-library -emit-llvm -disable-llvm-passes -o - %s | 
FileCheck %s --check-prefix=FLAG
+// RUN: %clang_cc1 -finclude-default-header -triple 
dxil-pc-shadermodel6.3-library -emit-llvm -disable-

[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-04-09 Thread Deric C. via cfe-commits


@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -res-may-alias -finclude-default-header -triple 
dxil-pc-shadermodel6.3-library -emit-llvm -disable-llvm-passes -o - %s | 
FileCheck %s --check-prefix=FLAG

Icohedron wrote:

Yea I will change these to clang_dxc

https://github.com/llvm/llvm-project/pull/131070
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-04-09 Thread Farzon Lotfi via cfe-commits


@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -res-may-alias -finclude-default-header -triple 
dxil-pc-shadermodel6.3-library -emit-llvm -disable-llvm-passes -o - %s | 
FileCheck %s --check-prefix=FLAG

farzonl wrote:

did you want to change these to `clang_dxc` aswell since you are making a dxc 
driver change?

https://github.com/llvm/llvm-project/pull/131070
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-03-18 Thread Finn Plummer via cfe-commits

https://github.com/inbelic edited 
https://github.com/llvm/llvm-project/pull/131070
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-03-17 Thread Finn Plummer via cfe-commits


@@ -0,0 +1,39 @@
+; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
+
+target triple = "dxil-pc-shadermodel6.8-library"
+
+; CHECK:  Combined Shader Flags for Module
+; CHECK-NEXT: Shader Flags Value: 0x20010
+
+; CHECK: Note: extra DXIL module flags:
+; CHECK:   Raw and Structured buffers
+; CHECK:   Any UAV may not alias any other UAV
+;
+
+; CHECK: Function loadUAV : 0x2000
+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 : 0x0010
+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}

inbelic wrote:

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

https://github.com/llvm/llvm-project/pull/131070
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-03-17 Thread Finn Plummer via cfe-commits


@@ -176,10 +198,16 @@ 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)) {

inbelic wrote:

We should adhere to: https://llvm.org/docs/CodingStandards.html#id60

https://github.com/llvm/llvm-project/pull/131070
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-03-17 Thread Finn Plummer via cfe-commits




inbelic wrote:

Sure, although I would maybe argue that setting the module flag with a comment 
makes it more clear which shader flag we are interested in testing here.

Just a suggestion though 

https://github.com/llvm/llvm-project/pull/131070
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-03-17 Thread Finn Plummer via cfe-commits

https://github.com/inbelic commented:

Nice work, just some minor comments/questions.

As mentioned, we should have a definitive answer to if we need SRV to be 
included at all here (both in code comments and testcases).

https://github.com/llvm/llvm-project/pull/131070
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-03-17 Thread Finn Plummer via cfe-commits


@@ -176,10 +198,16 @@ 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)) {

inbelic wrote:

style nit: remove braces

https://github.com/llvm/llvm-project/pull/131070
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-03-17 Thread Finn Plummer via cfe-commits


@@ -0,0 +1,39 @@
+; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
+
+target triple = "dxil-pc-shadermodel6.8-library"
+
+; CHECK:  Combined Shader Flags for Module
+; CHECK-NEXT: Shader Flags Value: 0x20010

inbelic wrote:

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

https://github.com/llvm/llvm-project/pull/131070
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-03-17 Thread Finn Plummer via cfe-commits




inbelic wrote:

Are we able to add the `-res-may-alias` flag here so that we keep our testcase 
isolated on the current flag being tested? Or add the module flag I suppose

https://github.com/llvm/llvm-project/pull/131070
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-03-17 Thread Finn Plummer via cfe-commits




inbelic wrote:

Can we add a comment in these test files to describe what they are distinctly 
testing? It is not immediately clear to me

https://github.com/llvm/llvm-project/pull/131070
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-03-15 Thread Deric C. via cfe-commits

https://github.com/Icohedron edited 
https://github.com/llvm/llvm-project/pull/131070
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-03-14 Thread Deric C. via cfe-commits

https://github.com/Icohedron edited 
https://github.com/llvm/llvm-project/pull/131070
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-03-14 Thread Deric C. via cfe-commits

https://github.com/Icohedron edited 
https://github.com/llvm/llvm-project/pull/131070
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-03-14 Thread Deric C. via cfe-commits

https://github.com/Icohedron updated 
https://github.com/llvm/llvm-project/pull/131070

>From 81196e016dbf1209637dd13315efff7eac461d42 Mon Sep 17 00:00:00 2001
From: Icohedron 
Date: Fri, 14 Mar 2025 00:24:26 +
Subject: [PATCH 1/3] Implement ResMayNotAlias DXIL shader flag analysis

---
 clang/include/clang/Basic/CodeGenOptions.def  |  3 ++
 clang/include/clang/Driver/Options.td |  5 +++
 clang/lib/CodeGen/CGHLSLRuntime.cpp   |  3 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  1 +
 clang/test/CodeGenHLSL/res-may-alias.hlsl |  7 +++
 llvm/lib/Target/DirectX/DXILShaderFlags.cpp   | 43 ---
 llvm/lib/Target/DirectX/DXILShaderFlags.h |  9 +++-
 .../DirectX/ShaderFlags/res-may-alias-0.ll| 39 +
 .../DirectX/ShaderFlags/res-may-alias-1.ll| 37 
 .../res-may-not-alias-shadermodel6.7.ll   | 33 ++
 .../res-may-not-alias-shadermodel6.8.ll   | 33 ++
 .../typed-uav-load-additional-formats.ll  |  9 ++--
 12 files changed, 210 insertions(+), 12 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/res-may-alias.hlsl
 create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-0.ll
 create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-1.ll
 create mode 100644 
llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-shadermodel6.7.ll
 create mode 100644 
llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-shadermodel6.8.ll

diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index a7f5f1abbb825..a436c0ec98d5b 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -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"
 
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 66ae8f1c7f064..9c5fd2354f95e 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -9043,6 +9043,11 @@ def dxil_validator_version : Option<["/", "-"], 
"validator-version", KIND_SEPARA
   HelpText<"Override validator version for module. Format: ;"
"Default: DXIL.dll version or current internal version">,
   MarshallingInfoString, "\"1.8\"">;
+def res_may_alias : Option<["/", "-"], "res-may-alias", KIND_FLAG>,
+  Group, Flags<[HelpHidden]>,
+  Visibility<[DXCOption, ClangOption, CC1Option]>,
+  HelpText<"Assume that UAVs/SRVs may alias">,
+  MarshallingInfoFlag>;
 def target_profile : DXCJoinedOrSeparate<"T">, MetaVarName<"">,
   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,"
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp 
b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 5916fa6183a27..6fd8bc295e8ca 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -283,10 +283,13 @@ void CGHLSLRuntime::addHLSLBufferLayoutType(const 
RecordType *StructType,
 
 void CGHLSLRuntime::finishCodeGen() {
   auto &TargetOpts = CGM.getTarget().getTargetOpts();
+  auto &CodeGenOpts = CGM.getCodeGenOpts();
   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);
 
   generateGlobalCtorDtorCalls();
 }
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index fe172d923ac07..9be3939641cfc 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -3959,6 +3959,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,
diff --git a/clang/test/CodeGenHLSL/res-may-alias.hlsl 
b/clang/test/CodeGenHLSL/res-may-alias.hlsl
new file mode 100644
index 0..53ee8ee4935d8
--- /dev/null
+++ b/clang/test/CodeGenHLSL/res-may-alias.hlsl
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -res-may-alias -finclude-default-header -triple 
dxil-pc-shadermodel6.3-library -emit-llvm -disable-llvm-passes -o - %s | 
FileCheck %s --check-prefix=FLAG
+// RUN: %clang_cc1 -finclude-default-header -triple 
dxil-pc-shadermodel6.3-library -emit-llvm -disable-

[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-03-14 Thread via cfe-commits

llvmbot wrote:



@llvm/pr-subscribers-backend-directx

@llvm/pr-subscribers-clang-driver

Author: Deric C. (Icohedron)


Changes

Fixes #112270

Completed ACs:
- `-res-may-alias` clang-dxc command-line option added
  - It inserts and sets a module metadata flag `dx.resmayalias` to 1
- Shader flag set appropriately:
  - CASE 1: command-line option -res-may-alias is NOT specified AND DXIL 
Version > 1.7 AND function uses UAVs
  - CASE 2: command-line option -res-may-alias is NOT specified AND DXIL 
Version <= 1.7 AND UAVs present globally
- Add tests 
  - A test (`res-may-not-alias-shadermodel6.8.ll`) for CASE 1
  - A test (`res-may-not-alias-shadermodel6.7.ll`) for CASE 2
  - A frontend test (`res-may-alias.hlsl`) for testing that the command-line 
option `-res-may-alias` sets the module metadata flag `dx.resmayalias` to 1, 
and does not set the module metadata flag `dx.resmayalias` if `-res-may-alias` 
is not given
  - Tests (`res-may-alias-0.ll`/`res-may-alias-1.ll`) for when the module 
metadata flag `dx.resmayalias` is set to 0 or 1



---
Full diff: https://github.com/llvm/llvm-project/pull/131070.diff


12 Files Affected:

- (modified) clang/include/clang/Basic/CodeGenOptions.def (+3) 
- (modified) clang/include/clang/Driver/Options.td (+5) 
- (modified) clang/lib/CodeGen/CGHLSLRuntime.cpp (+3) 
- (modified) clang/lib/Driver/ToolChains/Clang.cpp (+1) 
- (added) clang/test/CodeGenHLSL/res-may-alias.hlsl (+7) 
- (modified) llvm/lib/Target/DirectX/DXILShaderFlags.cpp (+37-6) 
- (modified) llvm/lib/Target/DirectX/DXILShaderFlags.h (+7-2) 
- (added) llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-0.ll (+39) 
- (added) llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-1.ll (+37) 
- (added) 
llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-shadermodel6.7.ll (+33) 
- (added) 
llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-shadermodel6.8.ll (+33) 
- (modified) 
llvm/test/CodeGen/DirectX/ShaderFlags/typed-uav-load-additional-formats.ll 
(+5-4) 


``diff
diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index a7f5f1abbb825..a436c0ec98d5b 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -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"
 
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 66ae8f1c7f064..9c5fd2354f95e 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -9043,6 +9043,11 @@ def dxil_validator_version : Option<["/", "-"], 
"validator-version", KIND_SEPARA
   HelpText<"Override validator version for module. Format: ;"
"Default: DXIL.dll version or current internal version">,
   MarshallingInfoString, "\"1.8\"">;
+def res_may_alias : Option<["/", "-"], "res-may-alias", KIND_FLAG>,
+  Group, Flags<[HelpHidden]>,
+  Visibility<[DXCOption, ClangOption, CC1Option]>,
+  HelpText<"Assume that UAVs/SRVs may alias">,
+  MarshallingInfoFlag>;
 def target_profile : DXCJoinedOrSeparate<"T">, MetaVarName<"">,
   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,"
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp 
b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 5916fa6183a27..6fd8bc295e8ca 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -283,10 +283,13 @@ void CGHLSLRuntime::addHLSLBufferLayoutType(const 
RecordType *StructType,
 
 void CGHLSLRuntime::finishCodeGen() {
   auto &TargetOpts = CGM.getTarget().getTargetOpts();
+  auto &CodeGenOpts = CGM.getCodeGenOpts();
   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);
 
   generateGlobalCtorDtorCalls();
 }
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index fe172d923ac07..9be3939641cfc 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -3959,6 +3959,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,
  optio

[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-03-13 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-codegen

Author: Deric C. (Icohedron)


Changes

Fixes #112270

Completed ACs:
- `-res-may-alias` clang-dxc command-line option added
  - It inserts and sets a module metadata flag `dx.resmayalias` to 1
- Shader flag set appropriately:
  - CASE 1: command-line option -res-may-alias is NOT specified AND DXIL 
Version > 1.7 AND function uses UAVs
  - CASE 2: command-line option -res-may-alias is NOT specified AND DXIL 
Version <= 1.7 AND UAVs present globally
- Add tests 
  - A test (`res-may-not-alias-shadermodel6.8.ll`) for CASE 1
  - A test (`res-may-not-alias-shadermodel6.7.ll`) for CASE 2
  - A frontend test (`res-may-alias.hlsl`) for testing that the command-line 
option `-res-may-alias` sets the module metadata flag `dx.resmayalias` to 1, 
and does not set the module metadata flag `dx.resmayalias` if `-res-may-alias` 
is not given
  - Tests (`res-may-alias-0.ll`/`res-may-alias-1.ll`) for when the module 
metadata flag `dx.resmayalias` is set to 0 or 1



---
Full diff: https://github.com/llvm/llvm-project/pull/131070.diff


12 Files Affected:

- (modified) clang/include/clang/Basic/CodeGenOptions.def (+3) 
- (modified) clang/include/clang/Driver/Options.td (+5) 
- (modified) clang/lib/CodeGen/CGHLSLRuntime.cpp (+3) 
- (modified) clang/lib/Driver/ToolChains/Clang.cpp (+1) 
- (added) clang/test/CodeGenHLSL/res-may-alias.hlsl (+7) 
- (modified) llvm/lib/Target/DirectX/DXILShaderFlags.cpp (+37-6) 
- (modified) llvm/lib/Target/DirectX/DXILShaderFlags.h (+7-2) 
- (added) llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-0.ll (+39) 
- (added) llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-1.ll (+37) 
- (added) 
llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-shadermodel6.7.ll (+33) 
- (added) 
llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-shadermodel6.8.ll (+33) 
- (modified) 
llvm/test/CodeGen/DirectX/ShaderFlags/typed-uav-load-additional-formats.ll 
(+5-4) 


``diff
diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index a7f5f1abbb825..a436c0ec98d5b 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -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"
 
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 66ae8f1c7f064..9c5fd2354f95e 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -9043,6 +9043,11 @@ def dxil_validator_version : Option<["/", "-"], 
"validator-version", KIND_SEPARA
   HelpText<"Override validator version for module. Format: ;"
"Default: DXIL.dll version or current internal version">,
   MarshallingInfoString, "\"1.8\"">;
+def res_may_alias : Option<["/", "-"], "res-may-alias", KIND_FLAG>,
+  Group, Flags<[HelpHidden]>,
+  Visibility<[DXCOption, ClangOption, CC1Option]>,
+  HelpText<"Assume that UAVs/SRVs may alias">,
+  MarshallingInfoFlag>;
 def target_profile : DXCJoinedOrSeparate<"T">, MetaVarName<"">,
   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,"
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp 
b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 5916fa6183a27..6fd8bc295e8ca 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -283,10 +283,13 @@ void CGHLSLRuntime::addHLSLBufferLayoutType(const 
RecordType *StructType,
 
 void CGHLSLRuntime::finishCodeGen() {
   auto &TargetOpts = CGM.getTarget().getTargetOpts();
+  auto &CodeGenOpts = CGM.getCodeGenOpts();
   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);
 
   generateGlobalCtorDtorCalls();
 }
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index fe172d923ac07..9be3939641cfc 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -3959,6 +3959,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,
diff --git a/clang/test/C

[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-03-13 Thread Deric C. via cfe-commits

https://github.com/Icohedron edited 
https://github.com/llvm/llvm-project/pull/131070
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-03-12 Thread Deric C. via cfe-commits

https://github.com/Icohedron created 
https://github.com/llvm/llvm-project/pull/131070

Fixes #112270

Completed ACs:
- `-res-may-alias` clang-dxc command-line option added
- Shader flag set appropriately:
  - CASE 1: command-line option -res-may-alias is NOT specified AND DXIL 
Version > 1.7 AND function uses UAVs
- Add tests 
  - A test (`res-may-not-alias-shadermodel6.8.ll`) for CASE 1
  - A test (`res-may-not-alias-shadermodel6.7.ll`) for CASE 2
  - A test (`res-may-alias.ll`) for the case where the command-line option 
`-res-may-alias` is specified
 
ACs left to complete:
- Shader flag set appropriately:
  - CASE 2: command-line option -res-may-alias is NOT specified AND DXIL 
Version <= 1.7 AND UAVs present globally
- This Draft PR currently uses the now-removed `DXILResourceMD` analysis 
(#130323) to query for global UAVs
- Need to create an alternative implementation for this case

>From ad5fbee6d0d58df1884153bb70e59a1434953659 Mon Sep 17 00:00:00 2001
From: Icohedron 
Date: Thu, 13 Mar 2025 03:15:39 +
Subject: [PATCH] Initial ResMayNotAlias shader flag implementation

---
 clang/include/clang/Basic/CodeGenOptions.def  |  3 ++
 clang/include/clang/Driver/Options.td |  5 +++
 clang/lib/CodeGen/CGHLSLRuntime.cpp   |  3 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  1 +
 llvm/lib/Target/DirectX/DXILShaderFlags.cpp   | 30 +---
 llvm/lib/Target/DirectX/DXILShaderFlags.h |  9 +++--
 .../DirectX/ShaderFlags/res-may-alias.ll  | 34 +++
 .../res-may-not-alias-shadermodel6.7.ll   | 33 ++
 .../res-may-not-alias-shadermodel6.8.ll   | 33 ++
 9 files changed, 145 insertions(+), 6 deletions(-)
 create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias.ll
 create mode 100644 
llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-shadermodel6.7.ll
 create mode 100644 
llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-shadermodel6.8.ll

diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index a7f5f1abbb825..a436c0ec98d5b 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -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"
 
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index d0414aba35209..9d33994c777d1 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -9044,6 +9044,11 @@ def dxil_validator_version : Option<["/", "-"], 
"validator-version", KIND_SEPARA
   HelpText<"Override validator version for module. Format: ;"
"Default: DXIL.dll version or current internal version">,
   MarshallingInfoString, "\"1.8\"">;
+def res_may_alias : Option<["/", "-"], "res-may-alias", KIND_FLAG>,
+  Group, Flags<[HelpHidden]>,
+  Visibility<[DXCOption, ClangOption, CC1Option]>,
+  HelpText<"Assume that UAVs/SRVs may alias">,
+  MarshallingInfoFlag>;
 def target_profile : DXCJoinedOrSeparate<"T">, MetaVarName<"">,
   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,"
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp 
b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index dc34653e8f497..b6c2cb0bfefc0 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -252,10 +252,13 @@ void CGHLSLRuntime::addHLSLBufferLayoutType(const 
RecordType *StructType,
 
 void CGHLSLRuntime::finishCodeGen() {
   auto &TargetOpts = CGM.getTarget().getTargetOpts();
+  auto &CodeGenOpts = CGM.getCodeGenOpts();
   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);
 
   generateGlobalCtorDtorCalls();
 }
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 4ebbd241d2f0b..f066e333c6e74 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -3960,6 +3960,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,
diff --git a/l

[clang] [llvm] [DirectX] Implement Shader Flags Analysis for ResMayNotAlias (PR #131070)

2025-03-12 Thread Deric C. via cfe-commits

https://github.com/Icohedron edited 
https://github.com/llvm/llvm-project/pull/131070
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits