[llvm-branch-commits] [clang] [llvm] [HLSL] Add resource constructor with implicit binding for global resources (PR #138976)

2025-05-12 Thread Ashley Coleman via llvm-branch-commits


@@ -55,11 +55,33 @@ export void foo() {
 // CHECK-SAME: i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3)
 // CHECK-NEXT: ret void
 
-// Buf2 initialization part 1 - FIXME: constructor with implicit binding does 
not exist yet; 
-// the global init function currently calls the default RWByteAddressBuffer C1 
constructor
-// CHECK: define internal void @__cxx_global_var_init.1()
+// Buf2 initialization part 1 - global init function that calls 
RWByteAddressBuffer C1 constructor with implicit binding
+// CHECK: define internal void @__cxx_global_var_init.1() #0 {
 // CHECK-NEXT: entry:
-// CHECK-NEXT: call void @_ZN4hlsl19RWByteAddressBufferC1Ev(ptr noundef 
nonnull align 4 dereferenceable(4) @_ZL4Buf2)
+// CHECK-NEXT: call void @_ZN4hlsl19RWByteAddressBufferC1Ejijj(ptr noundef 
nonnull align 4 dereferenceable(4) @_ZL4Buf2,
+// CHECK-SAME: i32 noundef 0, i32 noundef 1, i32 noundef 0, i32 noundef 0)
+
+// Buf2 initialization part 2 - body of RWByteAddressBuffer C1 constructor 
with implicit binding that calls the C2 constructor
+// CHECK: define linkonce_odr void @_ZN4hlsl19RWByteAddressBufferC1Ejijj(ptr 
noundef nonnull align 4 dereferenceable(4) %this,
+// CHECK-SAME: i32 noundef %spaceNo, i32 noundef %range, i32 noundef %index, 
i32 noundef %order_id)
+// CHECK-NEXT: entry:
+// CHECK-NEXT: %this.addr = alloca ptr, align 4

V-FEXrt wrote:

Is this test too fragile? In other tests we don't actually want to verify all 
the `alloca`/`load`s since they aren't actually doing the work we care about 
and will get optimized away. Is that the case here too?

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


[llvm-branch-commits] [clang] [llvm] [HLSL] Add resource constructor with implicit binding for global resources (PR #138976)

2025-05-12 Thread Ashley Coleman via llvm-branch-commits


@@ -3269,27 +3285,42 @@ static bool initVarDeclWithCtor(Sema &S, VarDecl *VD,
   return true;
 }
 
-static bool initGlobalResourceDecl(Sema &S, VarDecl *VD) {
+bool SemaHLSL::initGlobalResourceDecl(VarDecl *VD) {
+  std::optional RegisterSlot;
+  uint32_t SpaceNo = 0;
   HLSLResourceBindingAttr *RBA = VD->getAttr();
-  if (!RBA || !RBA->hasRegisterSlot())
-// FIXME: add support for implicit binding (llvm/llvm-project#110722)
-return false;
+  if (RBA) {
+if (RBA->hasRegisterSlot())
+  RegisterSlot = RBA->getSlotNumber();
+SpaceNo = RBA->getSpaceNumber();
+  }
 
-  ASTContext &AST = S.getASTContext();
+  ASTContext &AST = SemaRef.getASTContext();
   uint64_t UIntTySize = AST.getTypeSize(AST.UnsignedIntTy);
   uint64_t IntTySize = AST.getTypeSize(AST.IntTy);
-  Expr *Args[] = {
-  IntegerLiteral::Create(AST, llvm::APInt(UIntTySize, 
RBA->getSlotNumber()),
- AST.UnsignedIntTy, SourceLocation()),
-  IntegerLiteral::Create(AST,
- llvm::APInt(UIntTySize, RBA->getSpaceNumber()),
- AST.UnsignedIntTy, SourceLocation()),
-  IntegerLiteral::Create(AST, llvm::APInt(IntTySize, 1), AST.IntTy,
- SourceLocation()),
-  IntegerLiteral::Create(AST, llvm::APInt(UIntTySize, 0), 
AST.UnsignedIntTy,
- SourceLocation())};
-
-  return initVarDeclWithCtor(S, VD, Args);
+  IntegerLiteral *One = IntegerLiteral::Create(AST, llvm::APInt(IntTySize, 1),
+   AST.IntTy, SourceLocation());
+  IntegerLiteral *Zero = IntegerLiteral::Create(
+  AST, llvm::APInt(UIntTySize, 0), AST.UnsignedIntTy, SourceLocation());
+  IntegerLiteral *Space =
+  IntegerLiteral::Create(AST, llvm::APInt(UIntTySize, SpaceNo),
+ AST.UnsignedIntTy, SourceLocation());
+
+  // resource with explicit binding

V-FEXrt wrote:

This comment is duplicated here and 3318. Is that intentional? 

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


[llvm-branch-commits] [clang] [llvm] [HLSL] Add resource constructor with implicit binding for global resources (PR #138976)

2025-05-12 Thread Ashley Coleman via llvm-branch-commits

https://github.com/V-FEXrt approved this pull request.


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


[llvm-branch-commits] [clang] [llvm] [HLSL] Add resource constructor with implicit binding for global resources (PR #138976)

2025-05-12 Thread Ashley Coleman via llvm-branch-commits


@@ -668,6 +668,26 @@ BuiltinTypeDeclBuilder::addHandleConstructorFromBinding() {
   .finalize();
 }
 
+BuiltinTypeDeclBuilder &
+BuiltinTypeDeclBuilder::addHandleConstructorFromImplicitBinding() {
+  if (Record->isCompleteDefinition())

V-FEXrt wrote:

Could you explain why this is needed?

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


[llvm-branch-commits] [clang] [llvm] [HLSL] Add resource constructor with implicit binding for global resources (PR #138976)

2025-05-12 Thread Ashley Coleman via llvm-branch-commits


@@ -668,6 +668,26 @@ BuiltinTypeDeclBuilder::addHandleConstructorFromBinding() {
   .finalize();
 }
 
+BuiltinTypeDeclBuilder &
+BuiltinTypeDeclBuilder::addHandleConstructorFromImplicitBinding() {
+  if (Record->isCompleteDefinition())
+return *this;
+
+  using PH = BuiltinTypeMethodBuilder::PlaceHolder;
+  ASTContext &AST = SemaRef.getASTContext();
+  QualType HandleType = getResourceHandleField()->getType();
+
+  return BuiltinTypeMethodBuilder(*this, "", AST.VoidTy, false, true)
+  .addParam("spaceNo", AST.UnsignedIntTy)
+  .addParam("range", AST.IntTy)
+  .addParam("index", AST.UnsignedIntTy)
+  .addParam("order_id", AST.UnsignedIntTy)

V-FEXrt wrote:

nit: `spaceNo` and `order_id` are both in different styles. Can we align them?

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


[llvm-branch-commits] [clang] [llvm] [HLSL] Add resource constructor with implicit binding for global resources (PR #138976)

2025-05-09 Thread Helena Kotas via llvm-branch-commits

https://github.com/hekota updated 
https://github.com/llvm/llvm-project/pull/138976

>From dfce6ce8edc520251c0561cfd50cc6c11af46ecd Mon Sep 17 00:00:00 2001
From: Helena Kotas 
Date: Wed, 7 May 2025 14:43:27 -0700
Subject: [PATCH] [HLSL] Add resource constructor with implicit binding for
 global resources

Adds constructor for resources with implicit binding and applies it to
all resources without binding at the global scope. Adds Clang buildin
function __builtin_hlsl_resource_handlefromimplicitbinding that it translated
to llvm.dx|spv.resource.handlefromimplicitbinding calls. Specific bindings are
assigned in DXILResourceImplicitBinding pass.

Depends on #138043

Closes #136784
---
 clang/include/clang/Basic/Builtins.td |  6 ++
 clang/include/clang/Sema/SemaHLSL.h   |  7 ++
 clang/lib/CodeGen/CGHLSLBuiltins.cpp  | 15 
 clang/lib/CodeGen/CGHLSLRuntime.h |  2 +
 clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp | 20 ++
 clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.h   |  3 +-
 clang/lib/Sema/HLSLExternalSemaSource.cpp |  3 +-
 clang/lib/Sema/SemaHLSL.cpp   | 72 +--
 .../test/AST/HLSL/ByteAddressBuffers-AST.hlsl | 22 ++
 .../test/AST/HLSL/StructuredBuffers-AST.hlsl  | 22 ++
 clang/test/AST/HLSL/TypedBuffers-AST.hlsl | 22 ++
 .../CodeGenHLSL/GlobalConstructorLib.hlsl |  2 +-
 .../ByteAddressBuffers-constructors.hlsl  | 55 --
 .../builtins/RWBuffer-constructor.hlsl| 55 --
 .../StructuredBuffers-constructors.hlsl   | 54 +-
 .../StructuredBuffers-methods-ps.hlsl |  6 +-
 clang/test/CodeGenHLSL/static-local-ctor.hlsl |  2 +-
 llvm/include/llvm/IR/IntrinsicsSPIRV.td   |  5 ++
 18 files changed, 335 insertions(+), 38 deletions(-)

diff --git a/clang/include/clang/Basic/Builtins.td 
b/clang/include/clang/Basic/Builtins.td
index 11b1e247237a7..187d3b5ed24a7 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -4819,6 +4819,12 @@ def HLSLResourceHandleFromBinding : 
LangBuiltin<"HLSL_LANG"> {
   let Prototype = "void(...)";
 }
 
+def HLSLResourceHandleFromImplicitBinding : LangBuiltin<"HLSL_LANG"> {
+  let Spellings = ["__builtin_hlsl_resource_handlefromimplicitbinding"];
+  let Attributes = [NoThrow];
+  let Prototype = "void(...)";
+}
+
 def HLSLAll : LangBuiltin<"HLSL_LANG"> {
   let Spellings = ["__builtin_hlsl_all"];
   let Attributes = [NoThrow, Const];
diff --git a/clang/include/clang/Sema/SemaHLSL.h 
b/clang/include/clang/Sema/SemaHLSL.h
index 5d260acf92abb..bedf541439dbf 100644
--- a/clang/include/clang/Sema/SemaHLSL.h
+++ b/clang/include/clang/Sema/SemaHLSL.h
@@ -174,6 +174,8 @@ class SemaHLSL : public SemaBase {
   // buffer which will be created at the end of the translation unit.
   llvm::SmallVector DefaultCBufferDecls;
 
+  uint32_t ImplicitBindingNextOrderID = 0;
+
 private:
   void collectResourceBindingsOnVarDecl(VarDecl *D);
   void collectResourceBindingsOnUserRecordDecl(const VarDecl *VD,
@@ -181,6 +183,11 @@ class SemaHLSL : public SemaBase {
   void processExplicitBindingsOnDecl(VarDecl *D);
 
   void diagnoseAvailabilityViolations(TranslationUnitDecl *TU);
+
+  bool initGlobalResourceDecl(VarDecl *VD);
+  uint32_t getNextImplicitBindingOrderID() {
+return ImplicitBindingNextOrderID++;
+  }
 };
 
 } // namespace clang
diff --git a/clang/lib/CodeGen/CGHLSLBuiltins.cpp 
b/clang/lib/CodeGen/CGHLSLBuiltins.cpp
index 5d93df34c66b2..d4a0714da07b3 100644
--- a/clang/lib/CodeGen/CGHLSLBuiltins.cpp
+++ b/clang/lib/CodeGen/CGHLSLBuiltins.cpp
@@ -303,6 +303,21 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned 
BuiltinID,
 HandleTy, CGM.getHLSLRuntime().getCreateHandleFromBindingIntrinsic(),
 ArrayRef{SpaceOp, RegisterOp, RangeOp, IndexOp, NonUniform});
   }
+  case Builtin::BI__builtin_hlsl_resource_handlefromimplicitbinding: {
+llvm::Type *HandleTy = CGM.getTypes().ConvertType(E->getType());
+Value *SpaceOp = EmitScalarExpr(E->getArg(1));
+Value *RangeOp = EmitScalarExpr(E->getArg(2));
+Value *IndexOp = EmitScalarExpr(E->getArg(3));
+Value *OrderID = EmitScalarExpr(E->getArg(4));
+// FIXME: NonUniformResourceIndex bit is not yet implemented
+// (llvm/llvm-project#135452)
+Value *NonUniform =
+llvm::ConstantInt::get(llvm::Type::getInt1Ty(getLLVMContext()), false);
+return Builder.CreateIntrinsic(
+HandleTy,
+CGM.getHLSLRuntime().getCreateHandleFromImplicitBindingIntrinsic(),
+ArrayRef{OrderID, SpaceOp, RangeOp, IndexOp, NonUniform});
+  }
   case Builtin::BI__builtin_hlsl_all: {
 Value *Op0 = EmitScalarExpr(E->getArg(0));
 return Builder.CreateIntrinsic(
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h 
b/clang/lib/CodeGen/CGHLSLRuntime.h
index 4d6db3f5d9f3e..e40864d8ed854 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.h
+++ b/clang/lib/CodeGen/CGHLSLRuntime.h
@@ -119,6 +119,8 @@ class CGHLSLRuntime {
 

[llvm-branch-commits] [clang] [llvm] [HLSL] Add resource constructor with implicit binding for global resources (PR #138976)

2025-05-09 Thread Helena Kotas via llvm-branch-commits

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


[llvm-branch-commits] [clang] [llvm] [HLSL] Add resource constructor with implicit binding for global resources (PR #138976)

2025-05-09 Thread Helena Kotas via llvm-branch-commits

https://github.com/hekota updated 
https://github.com/llvm/llvm-project/pull/138976



  



Rate limit · GitHub


  body {
background-color: #f6f8fa;
color: #24292e;
font-family: -apple-system,BlinkMacSystemFont,Segoe 
UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;
font-size: 14px;
line-height: 1.5;
margin: 0;
  }

  .container { margin: 50px auto; max-width: 600px; text-align: center; 
padding: 0 24px; }

  a { color: #0366d6; text-decoration: none; }
  a:hover { text-decoration: underline; }

  h1 { line-height: 60px; font-size: 48px; font-weight: 300; margin: 0px; 
text-shadow: 0 1px 0 #fff; }
  p { color: rgba(0, 0, 0, 0.5); margin: 20px 0 40px; }

  ul { list-style: none; margin: 25px 0; padding: 0; }
  li { display: table-cell; font-weight: bold; width: 1%; }

  .logo { display: inline-block; margin-top: 35px; }
  .logo-img-2x { display: none; }
  @media
  only screen and (-webkit-min-device-pixel-ratio: 2),
  only screen and (   min--moz-device-pixel-ratio: 2),
  only screen and ( -o-min-device-pixel-ratio: 2/1),
  only screen and (min-device-pixel-ratio: 2),
  only screen and (min-resolution: 192dpi),
  only screen and (min-resolution: 2dppx) {
.logo-img-1x { display: none; }
.logo-img-2x { display: inline-block; }
  }

  #suggestions {
margin-top: 35px;
color: #ccc;
  }
  #suggestions a {
color: #66;
font-weight: 200;
font-size: 14px;
margin: 0 10px;
  }


  
  



  Whoa there!
  You have exceeded a secondary rate limit.
Please wait a few minutes before you try again;
in some cases this may take up to an hour.
  
  
https://support.github.com/contact";>Contact Support —
https://githubstatus.com";>GitHub Status —
https://twitter.com/githubstatus";>@githubstatus
  

  

  

  

  

  


___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [llvm] [HLSL] Add resource constructor with implicit binding for global resources (PR #138976)

2025-05-09 Thread Helena Kotas via llvm-branch-commits


@@ -32,6 +32,6 @@ export float TestLoad() {
 // CHECK: %[[PTR1:.*]] = call ptr 
@llvm.dx.resource.getpointer.p0.tdx.RawBuffer_f32_1_1t(target("dx.RawBuffer", 
float, 1, 1) %{{[0-9]+}}, i32 %{{[0-9]+}})
 // CHECK: %[[VALUE1:.*]] = load float, ptr %[[PTR1]]
 
-// CHECK: declare i32 
@llvm.dx.resource.updatecounter.tdx.RawBuffer_f32_1_0t(target("dx.RawBuffer", 
float, 1, 0), i8) #3
-// CHECK: declare i32 
@llvm.dx.resource.updatecounter.tdx.RawBuffer_f32_1_1t(target("dx.RawBuffer", 
float, 1, 1), i8) #3
-// CHECK: declare ptr 
@llvm.dx.resource.getpointer.p0.tdx.RawBuffer_f32_1_1t(target("dx.RawBuffer", 
float, 1, 1), i32) #4

hekota wrote:

I'll address this in a separate PR.

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


[llvm-branch-commits] [clang] [llvm] [HLSL] Add resource constructor with implicit binding for global resources (PR #138976)

2025-05-07 Thread Helena Kotas via llvm-branch-commits


@@ -21,7 +21,7 @@ void InitBuf(RWBuffer buf) {
 // CHECK-NEXT: br i1 [[Tmp3]]
 // CHECK-NOT: _Init_thread_header
 // CHECK: init.check:
-// CHECK-NEXT: call void @_ZN4hlsl8RWBufferIiEC1Ev
+// CHECK-NEXT: call void @_ZN4hlsl8RWBufferIiEC1Ejijj

hekota wrote:

Same as above - updates the RWBuffer init from the default constructor to the 
constructor with implicit binding.

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


[llvm-branch-commits] [clang] [llvm] [HLSL] Add resource constructor with implicit binding for global resources (PR #138976)

2025-05-07 Thread Helena Kotas via llvm-branch-commits


@@ -32,6 +32,6 @@ export float TestLoad() {
 // CHECK: %[[PTR1:.*]] = call ptr 
@llvm.dx.resource.getpointer.p0.tdx.RawBuffer_f32_1_1t(target("dx.RawBuffer", 
float, 1, 1) %{{[0-9]+}}, i32 %{{[0-9]+}})
 // CHECK: %[[VALUE1:.*]] = load float, ptr %[[PTR1]]
 
-// CHECK: declare i32 
@llvm.dx.resource.updatecounter.tdx.RawBuffer_f32_1_0t(target("dx.RawBuffer", 
float, 1, 0), i8) #3
-// CHECK: declare i32 
@llvm.dx.resource.updatecounter.tdx.RawBuffer_f32_1_1t(target("dx.RawBuffer", 
float, 1, 1), i8) #3
-// CHECK: declare ptr 
@llvm.dx.resource.getpointer.p0.tdx.RawBuffer_f32_1_1t(target("dx.RawBuffer", 
float, 1, 1), i32) #4

hekota wrote:

Removing these function attributes as the numbering is not stable.

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


[llvm-branch-commits] [clang] [llvm] [HLSL] Add resource constructor with implicit binding for global resources (PR #138976)

2025-05-07 Thread Helena Kotas via llvm-branch-commits


@@ -33,7 +33,7 @@ void SecondEntry() {}
 
 // Verify the constructor is alwaysinline
 // NOINLINE: ; Function Attrs: {{.*}}alwaysinline
-// NOINLINE-NEXT: define linkonce_odr void @_ZN4hlsl8RWBufferIfEC2Ev({{.*}} 
[[CtorAttr:\#[0-9]+]]
+// NOINLINE-NEXT: define linkonce_odr void @_ZN4hlsl8RWBufferIfEC2Ejijj({{.*}} 
[[CtorAttr:\#[0-9]+]]

hekota wrote:

This is changing the `RWBuffer Buffer;` initialization from the default 
constructor (init handle to poison value) to the constructor with implicit 
binding.

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


[llvm-branch-commits] [clang] [llvm] [HLSL] Add resource constructor with implicit binding for global resources (PR #138976)

2025-05-07 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-codegen

Author: Helena Kotas (hekota)


Changes

Adds constructor for resources with implicit binding and applies it to all 
resources without binding at the global scope.
Adds Clang builtin function `__builtin_hlsl_resource_handlefromimplicitbinding` 
that gets translated to `llvm.dx|spv.resource.handlefromimplicitbinding` 
intrinsic calls. Specific bindings are assigned in DXILResourceImplicitBinding 
pass.

Design proposals:
https://github.com/llvm/wg-hlsl/blob/main/proposals/0024-implicit-resource-binding.md
https://github.com/llvm/wg-hlsl/blob/main/proposals/0025-resource-constructors.md

One change from the proposals is that the `order_id` parameter is added onto 
the constructor. Originally it was supposed to be generated in codegen when the 
`llvm.dx|spv.resource.handlefromimplicitbinding` call is emitted, but that is 
not possible because the call is inside a constructor, and the constructor body 
is generated once per resource type and not resource instance. So the only way 
to inject instance-based data like `order_id` into the 
`llvm.dx|spv.resource.handlefromimplicitbinding` call is that it must come in 
via the constructor argument.

Depends on #138043

Closes #136784

---

Patch is 34.47 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/138976.diff


18 Files Affected:

- (modified) clang/include/clang/Basic/Builtins.td (+6) 
- (modified) clang/include/clang/Sema/SemaHLSL.h (+7) 
- (modified) clang/lib/CodeGen/CGHLSLBuiltins.cpp (+15) 
- (modified) clang/lib/CodeGen/CGHLSLRuntime.h (+2) 
- (modified) clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp (+20) 
- (modified) clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.h (+2-1) 
- (modified) clang/lib/Sema/HLSLExternalSemaSource.cpp (+2-1) 
- (modified) clang/lib/Sema/SemaHLSL.cpp (+52-20) 
- (modified) clang/test/AST/HLSL/ByteAddressBuffers-AST.hlsl (+22) 
- (modified) clang/test/AST/HLSL/StructuredBuffers-AST.hlsl (+22) 
- (modified) clang/test/AST/HLSL/TypedBuffers-AST.hlsl (+22) 
- (modified) clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl (+1-1) 
- (modified) 
clang/test/CodeGenHLSL/builtins/ByteAddressBuffers-constructors.hlsl (+51-4) 
- (modified) clang/test/CodeGenHLSL/builtins/RWBuffer-constructor.hlsl (+51-4) 
- (modified) 
clang/test/CodeGenHLSL/builtins/StructuredBuffers-constructors.hlsl (+51-3) 
- (modified) clang/test/CodeGenHLSL/builtins/StructuredBuffers-methods-ps.hlsl 
(+3-3) 
- (modified) clang/test/CodeGenHLSL/static-local-ctor.hlsl (+1-1) 
- (modified) llvm/include/llvm/IR/IntrinsicsSPIRV.td (+5) 


``diff
diff --git a/clang/include/clang/Basic/Builtins.td 
b/clang/include/clang/Basic/Builtins.td
index 11b1e247237a7..187d3b5ed24a7 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -4819,6 +4819,12 @@ def HLSLResourceHandleFromBinding : 
LangBuiltin<"HLSL_LANG"> {
   let Prototype = "void(...)";
 }
 
+def HLSLResourceHandleFromImplicitBinding : LangBuiltin<"HLSL_LANG"> {
+  let Spellings = ["__builtin_hlsl_resource_handlefromimplicitbinding"];
+  let Attributes = [NoThrow];
+  let Prototype = "void(...)";
+}
+
 def HLSLAll : LangBuiltin<"HLSL_LANG"> {
   let Spellings = ["__builtin_hlsl_all"];
   let Attributes = [NoThrow, Const];
diff --git a/clang/include/clang/Sema/SemaHLSL.h 
b/clang/include/clang/Sema/SemaHLSL.h
index 5d260acf92abb..bedf541439dbf 100644
--- a/clang/include/clang/Sema/SemaHLSL.h
+++ b/clang/include/clang/Sema/SemaHLSL.h
@@ -174,6 +174,8 @@ class SemaHLSL : public SemaBase {
   // buffer which will be created at the end of the translation unit.
   llvm::SmallVector DefaultCBufferDecls;
 
+  uint32_t ImplicitBindingNextOrderID = 0;
+
 private:
   void collectResourceBindingsOnVarDecl(VarDecl *D);
   void collectResourceBindingsOnUserRecordDecl(const VarDecl *VD,
@@ -181,6 +183,11 @@ class SemaHLSL : public SemaBase {
   void processExplicitBindingsOnDecl(VarDecl *D);
 
   void diagnoseAvailabilityViolations(TranslationUnitDecl *TU);
+
+  bool initGlobalResourceDecl(VarDecl *VD);
+  uint32_t getNextImplicitBindingOrderID() {
+return ImplicitBindingNextOrderID++;
+  }
 };
 
 } // namespace clang
diff --git a/clang/lib/CodeGen/CGHLSLBuiltins.cpp 
b/clang/lib/CodeGen/CGHLSLBuiltins.cpp
index 5d93df34c66b2..d4a0714da07b3 100644
--- a/clang/lib/CodeGen/CGHLSLBuiltins.cpp
+++ b/clang/lib/CodeGen/CGHLSLBuiltins.cpp
@@ -303,6 +303,21 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned 
BuiltinID,
 HandleTy, CGM.getHLSLRuntime().getCreateHandleFromBindingIntrinsic(),
 ArrayRef{SpaceOp, RegisterOp, RangeOp, IndexOp, NonUniform});
   }
+  case Builtin::BI__builtin_hlsl_resource_handlefromimplicitbinding: {
+llvm::Type *HandleTy = CGM.getTypes().ConvertType(E->getType());
+Value *SpaceOp = EmitScalarExpr(E->getArg(1));
+Value *RangeOp = EmitScalarExpr(E->getArg(2));
+Value *IndexOp = EmitScalarExpr(E->getArg(3));
+Value *OrderID = EmitScalar

[llvm-branch-commits] [clang] [llvm] [HLSL] Add resource constructor with implicit binding for global resources (PR #138976)

2025-05-07 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-hlsl

Author: Helena Kotas (hekota)


Changes

Adds constructor for resources with implicit binding and applies it to all 
resources without binding at the global scope.
Adds Clang builtin function `__builtin_hlsl_resource_handlefromimplicitbinding` 
that gets translated to `llvm.dx|spv.resource.handlefromimplicitbinding` 
intrinsic calls. Specific bindings are assigned in DXILResourceImplicitBinding 
pass.

Design proposals:
https://github.com/llvm/wg-hlsl/blob/main/proposals/0024-implicit-resource-binding.md
https://github.com/llvm/wg-hlsl/blob/main/proposals/0025-resource-constructors.md

One change from the proposals is that the `order_id` parameter is added onto 
the constructor. Originally it was supposed to be generated in codegen when the 
`llvm.dx|spv.resource.handlefromimplicitbinding` call is emitted, but that is 
not possible because the call is inside a constructor, and the constructor body 
is generated once per resource type and not resource instance. So the only way 
to inject instance-based data like `order_id` into the 
`llvm.dx|spv.resource.handlefromimplicitbinding` call is that it must come in 
via the constructor argument.

Depends on #138043

Closes #136784

---

Patch is 34.47 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/138976.diff


18 Files Affected:

- (modified) clang/include/clang/Basic/Builtins.td (+6) 
- (modified) clang/include/clang/Sema/SemaHLSL.h (+7) 
- (modified) clang/lib/CodeGen/CGHLSLBuiltins.cpp (+15) 
- (modified) clang/lib/CodeGen/CGHLSLRuntime.h (+2) 
- (modified) clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp (+20) 
- (modified) clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.h (+2-1) 
- (modified) clang/lib/Sema/HLSLExternalSemaSource.cpp (+2-1) 
- (modified) clang/lib/Sema/SemaHLSL.cpp (+52-20) 
- (modified) clang/test/AST/HLSL/ByteAddressBuffers-AST.hlsl (+22) 
- (modified) clang/test/AST/HLSL/StructuredBuffers-AST.hlsl (+22) 
- (modified) clang/test/AST/HLSL/TypedBuffers-AST.hlsl (+22) 
- (modified) clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl (+1-1) 
- (modified) 
clang/test/CodeGenHLSL/builtins/ByteAddressBuffers-constructors.hlsl (+51-4) 
- (modified) clang/test/CodeGenHLSL/builtins/RWBuffer-constructor.hlsl (+51-4) 
- (modified) 
clang/test/CodeGenHLSL/builtins/StructuredBuffers-constructors.hlsl (+51-3) 
- (modified) clang/test/CodeGenHLSL/builtins/StructuredBuffers-methods-ps.hlsl 
(+3-3) 
- (modified) clang/test/CodeGenHLSL/static-local-ctor.hlsl (+1-1) 
- (modified) llvm/include/llvm/IR/IntrinsicsSPIRV.td (+5) 


``diff
diff --git a/clang/include/clang/Basic/Builtins.td 
b/clang/include/clang/Basic/Builtins.td
index 11b1e247237a7..187d3b5ed24a7 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -4819,6 +4819,12 @@ def HLSLResourceHandleFromBinding : 
LangBuiltin<"HLSL_LANG"> {
   let Prototype = "void(...)";
 }
 
+def HLSLResourceHandleFromImplicitBinding : LangBuiltin<"HLSL_LANG"> {
+  let Spellings = ["__builtin_hlsl_resource_handlefromimplicitbinding"];
+  let Attributes = [NoThrow];
+  let Prototype = "void(...)";
+}
+
 def HLSLAll : LangBuiltin<"HLSL_LANG"> {
   let Spellings = ["__builtin_hlsl_all"];
   let Attributes = [NoThrow, Const];
diff --git a/clang/include/clang/Sema/SemaHLSL.h 
b/clang/include/clang/Sema/SemaHLSL.h
index 5d260acf92abb..bedf541439dbf 100644
--- a/clang/include/clang/Sema/SemaHLSL.h
+++ b/clang/include/clang/Sema/SemaHLSL.h
@@ -174,6 +174,8 @@ class SemaHLSL : public SemaBase {
   // buffer which will be created at the end of the translation unit.
   llvm::SmallVector DefaultCBufferDecls;
 
+  uint32_t ImplicitBindingNextOrderID = 0;
+
 private:
   void collectResourceBindingsOnVarDecl(VarDecl *D);
   void collectResourceBindingsOnUserRecordDecl(const VarDecl *VD,
@@ -181,6 +183,11 @@ class SemaHLSL : public SemaBase {
   void processExplicitBindingsOnDecl(VarDecl *D);
 
   void diagnoseAvailabilityViolations(TranslationUnitDecl *TU);
+
+  bool initGlobalResourceDecl(VarDecl *VD);
+  uint32_t getNextImplicitBindingOrderID() {
+return ImplicitBindingNextOrderID++;
+  }
 };
 
 } // namespace clang
diff --git a/clang/lib/CodeGen/CGHLSLBuiltins.cpp 
b/clang/lib/CodeGen/CGHLSLBuiltins.cpp
index 5d93df34c66b2..d4a0714da07b3 100644
--- a/clang/lib/CodeGen/CGHLSLBuiltins.cpp
+++ b/clang/lib/CodeGen/CGHLSLBuiltins.cpp
@@ -303,6 +303,21 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned 
BuiltinID,
 HandleTy, CGM.getHLSLRuntime().getCreateHandleFromBindingIntrinsic(),
 ArrayRef{SpaceOp, RegisterOp, RangeOp, IndexOp, NonUniform});
   }
+  case Builtin::BI__builtin_hlsl_resource_handlefromimplicitbinding: {
+llvm::Type *HandleTy = CGM.getTypes().ConvertType(E->getType());
+Value *SpaceOp = EmitScalarExpr(E->getArg(1));
+Value *RangeOp = EmitScalarExpr(E->getArg(2));
+Value *IndexOp = EmitScalarExpr(E->getArg(3));
+Value *OrderID = EmitScalarExpr(E->g

[llvm-branch-commits] [clang] [llvm] [HLSL] Add resource constructor with implicit binding for global resources (PR #138976)

2025-05-07 Thread Helena Kotas via llvm-branch-commits

https://github.com/hekota created 
https://github.com/llvm/llvm-project/pull/138976

Adds constructor for resources with implicit binding and applies it to all 
resources without binding at the global scope.
Adds Clang builtin function `__builtin_hlsl_resource_handlefromimplicitbinding` 
that gets translated to `llvm.dx|spv.resource.handlefromimplicitbinding` 
intrinsic calls. Specific bindings are assigned in DXILResourceImplicitBinding 
pass.

Design proposals:
https://github.com/llvm/wg-hlsl/blob/main/proposals/0024-implicit-resource-binding.md
https://github.com/llvm/wg-hlsl/blob/main/proposals/0025-resource-constructors.md

One change from the proposals is that the `order_id` parameter is added onto 
the constructor. Originally it was supposed to be generated in codegen when the 
`llvm.dx|spv.resource.handlefromimplicitbinding` call is emitted, but that is 
not possible because the call is inside a constructor, and the constructor body 
is generated once per resource type and not resource instance. So the only way 
to inject instance-based data like `order_id` into the 
`llvm.dx|spv.resource.handlefromimplicitbinding` call is that it must come in 
via the constructor argument.

Depends on #138043

Closes #136784



  



Rate limit · GitHub


  body {
background-color: #f6f8fa;
color: #24292e;
font-family: -apple-system,BlinkMacSystemFont,Segoe 
UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;
font-size: 14px;
line-height: 1.5;
margin: 0;
  }

  .container { margin: 50px auto; max-width: 600px; text-align: center; 
padding: 0 24px; }

  a { color: #0366d6; text-decoration: none; }
  a:hover { text-decoration: underline; }

  h1 { line-height: 60px; font-size: 48px; font-weight: 300; margin: 0px; 
text-shadow: 0 1px 0 #fff; }
  p { color: rgba(0, 0, 0, 0.5); margin: 20px 0 40px; }

  ul { list-style: none; margin: 25px 0; padding: 0; }
  li { display: table-cell; font-weight: bold; width: 1%; }

  .logo { display: inline-block; margin-top: 35px; }
  .logo-img-2x { display: none; }
  @media
  only screen and (-webkit-min-device-pixel-ratio: 2),
  only screen and (   min--moz-device-pixel-ratio: 2),
  only screen and ( -o-min-device-pixel-ratio: 2/1),
  only screen and (min-device-pixel-ratio: 2),
  only screen and (min-resolution: 192dpi),
  only screen and (min-resolution: 2dppx) {
.logo-img-1x { display: none; }
.logo-img-2x { display: inline-block; }
  }

  #suggestions {
margin-top: 35px;
color: #ccc;
  }
  #suggestions a {
color: #66;
font-weight: 200;
font-size: 14px;
margin: 0 10px;
  }


  
  



  Whoa there!
  You have exceeded a secondary rate limit.
Please wait a few minutes before you try again;
in some cases this may take up to an hour.
  
  
https://support.github.com/contact";>Contact Support —
https://githubstatus.com";>GitHub Status —
https://twitter.com/githubstatus";>@githubstatus
  

  

  

  

  

  


___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits