Skip to content

[clang] Emit convergence tokens for loop in global array init#140120

Merged
s-perron merged 1 commit into
llvm:mainfrom
s-perron:array_init_convergence
May 15, 2025
Merged

[clang] Emit convergence tokens for loop in global array init#140120
s-perron merged 1 commit into
llvm:mainfrom
s-perron:array_init_convergence

Conversation

@s-perron

Copy link
Copy Markdown
Contributor

When initializing a global array, a loop is generated, but no
convergence is emitted for the loop. This fixes that up.

When initializing a global array, a loop is generated, but no
convergence is emitted for the loop. This fixes that up.
@s-perron s-perron requested review from Keenuts and cassiebeckley May 15, 2025 18:36
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. HLSL HLSL Language Support labels May 15, 2025
@llvmbot

llvmbot commented May 15, 2025

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-hlsl

Author: Steven Perron (s-perron)

Changes

When initializing a global array, a loop is generated, but no
convergence is emitted for the loop. This fixes that up.


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

2 Files Affected:

  • (modified) clang/lib/CodeGen/CGClass.cpp (+5)
  • (added) clang/test/CodeGenHLSL/convergence/global_array.hlsl (+16)
diff --git a/clang/lib/CodeGen/CGClass.cpp b/clang/lib/CodeGen/CGClass.cpp
index befbfc64a680c..44062739d5dd4 100644
--- a/clang/lib/CodeGen/CGClass.cpp
+++ b/clang/lib/CodeGen/CGClass.cpp
@@ -2042,6 +2042,8 @@ void CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *ctor,
   cur->addIncoming(arrayBegin, entryBB);
 
   // Inside the loop body, emit the constructor call on the array element.
+  if (CGM.shouldEmitConvergenceTokens())
+    ConvergenceTokenStack.push_back(emitConvergenceLoopToken(loopBB));
 
   // The alignment of the base, adjusted by the size of a single element,
   // provides a conservative estimate of the alignment of every element.
@@ -2101,6 +2103,9 @@ void CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *ctor,
   // Patch the earlier check to skip over the loop.
   if (zeroCheckBranch) zeroCheckBranch->setSuccessor(0, contBB);
 
+  if (CGM.shouldEmitConvergenceTokens())
+    ConvergenceTokenStack.pop_back();
+
   EmitBlock(contBB);
 }
 
diff --git a/clang/test/CodeGenHLSL/convergence/global_array.hlsl b/clang/test/CodeGenHLSL/convergence/global_array.hlsl
new file mode 100644
index 0000000000000..e11de82c3ded0
--- /dev/null
+++ b/clang/test/CodeGenHLSL/convergence/global_array.hlsl
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -Wno-hlsl-implicit-binding -finclude-default-header -triple spirv-unknown-vulkan-compute -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s
+
+// CHECK: define internal spir_func void @__cxx_global_var_init()
+// CHECK: [[entry_token:%.*]] = call token @llvm.experimental.convergence.entry()
+// CHECK: br label %[[loop_entry:.*]]
+
+// CHECK: [[loop_entry]]:
+// CHECK: [[loop_token:%.*]] = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token [[entry_token]]) ]
+// CHECK: call void {{.*}} [ "convergencectrl"(token [[loop_token]]) ]
+// CHECK: br i1 {{%.*}} label {{%.*}} label %[[loop_entry]]
+RWBuffer<float> e[2];
+
+[numthreads(4,1,1)]
+void main() {
+}
+

@llvmbot

llvmbot commented May 15, 2025

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-clang

Author: Steven Perron (s-perron)

Changes

When initializing a global array, a loop is generated, but no
convergence is emitted for the loop. This fixes that up.


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

2 Files Affected:

  • (modified) clang/lib/CodeGen/CGClass.cpp (+5)
  • (added) clang/test/CodeGenHLSL/convergence/global_array.hlsl (+16)
diff --git a/clang/lib/CodeGen/CGClass.cpp b/clang/lib/CodeGen/CGClass.cpp
index befbfc64a680c..44062739d5dd4 100644
--- a/clang/lib/CodeGen/CGClass.cpp
+++ b/clang/lib/CodeGen/CGClass.cpp
@@ -2042,6 +2042,8 @@ void CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *ctor,
   cur->addIncoming(arrayBegin, entryBB);
 
   // Inside the loop body, emit the constructor call on the array element.
+  if (CGM.shouldEmitConvergenceTokens())
+    ConvergenceTokenStack.push_back(emitConvergenceLoopToken(loopBB));
 
   // The alignment of the base, adjusted by the size of a single element,
   // provides a conservative estimate of the alignment of every element.
@@ -2101,6 +2103,9 @@ void CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *ctor,
   // Patch the earlier check to skip over the loop.
   if (zeroCheckBranch) zeroCheckBranch->setSuccessor(0, contBB);
 
+  if (CGM.shouldEmitConvergenceTokens())
+    ConvergenceTokenStack.pop_back();
+
   EmitBlock(contBB);
 }
 
diff --git a/clang/test/CodeGenHLSL/convergence/global_array.hlsl b/clang/test/CodeGenHLSL/convergence/global_array.hlsl
new file mode 100644
index 0000000000000..e11de82c3ded0
--- /dev/null
+++ b/clang/test/CodeGenHLSL/convergence/global_array.hlsl
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -Wno-hlsl-implicit-binding -finclude-default-header -triple spirv-unknown-vulkan-compute -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s
+
+// CHECK: define internal spir_func void @__cxx_global_var_init()
+// CHECK: [[entry_token:%.*]] = call token @llvm.experimental.convergence.entry()
+// CHECK: br label %[[loop_entry:.*]]
+
+// CHECK: [[loop_entry]]:
+// CHECK: [[loop_token:%.*]] = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token [[entry_token]]) ]
+// CHECK: call void {{.*}} [ "convergencectrl"(token [[loop_token]]) ]
+// CHECK: br i1 {{%.*}} label {{%.*}} label %[[loop_entry]]
+RWBuffer<float> e[2];
+
+[numthreads(4,1,1)]
+void main() {
+}
+

@s-perron s-perron merged commit 2e6433b into llvm:main May 15, 2025
15 checks passed
@s-perron s-perron deleted the array_init_convergence branch December 1, 2025 20:36
inbelic added a commit to inbelic/llvm-project that referenced this pull request Mar 26, 2026
This follows the previous correction made in llvm#140120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category HLSL HLSL Language Support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants