Skip to content

Conversation

@llvm-beanz
Copy link
Collaborator

HLSL doesn't distinguish main from any other function. It does treat entry points special, but they're not required to be called main so we have a different attribute annotation to mark them.

At the moment this change really just changes the mangling of functions named main in the Itanium mangling.

Fixes #110517

HLSL doesn't distinguish `main` from any other function. It does treat
entry points special, but they're not required to be called `main` so
we have a different attribute annotation to mark them.

At the moment this change really just changes the mangling of functions
named `main` in the Itanium mangling.

Fixes llvm#110517
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" HLSL HLSL Language Support labels Sep 30, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 30, 2024

@llvm/pr-subscribers-hlsl

@llvm/pr-subscribers-clang

Author: Chris B (llvm-beanz)

Changes

HLSL doesn't distinguish main from any other function. It does treat entry points special, but they're not required to be called main so we have a different attribute annotation to mark them.

At the moment this change really just changes the mangling of functions named main in the Itanium mangling.

Fixes #110517


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

3 Files Affected:

  • (modified) clang/lib/AST/Decl.cpp (+1)
  • (modified) clang/test/CodeGenHLSL/builtins/wave_get_lane_index_do_while.hlsl (+1-1)
  • (modified) clang/test/Options/enable_16bit_types_validation_spirv.hlsl (+1-1)
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index a14b1b33d35efc..84ef9f74582ef6 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -3302,6 +3302,7 @@ bool FunctionDecl::isImmediateFunction() const {
 
 bool FunctionDecl::isMain() const {
   return isNamed(this, "main") && !getLangOpts().Freestanding &&
+         !getLangOpts().HLSL &&
          (getDeclContext()->getRedeclContext()->isTranslationUnit() ||
           isExternC());
 }
diff --git a/clang/test/CodeGenHLSL/builtins/wave_get_lane_index_do_while.hlsl b/clang/test/CodeGenHLSL/builtins/wave_get_lane_index_do_while.hlsl
index 9481b0d60a2723..c223b9117e9277 100644
--- a/clang/test/CodeGenHLSL/builtins/wave_get_lane_index_do_while.hlsl
+++ b/clang/test/CodeGenHLSL/builtins/wave_get_lane_index_do_while.hlsl
@@ -1,7 +1,7 @@
 // RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \
 // RUN:   spirv-pc-vulkan-library %s -emit-llvm -disable-llvm-passes -o - | FileCheck %s
 
-// CHECK: define spir_func void @main() [[A0:#[0-9]+]] {
+// CHECK: define spir_func void @{{.*}}main{{.*}}() [[A0:#[0-9]+]] {
 void main() {
 // CHECK: entry:
 // CHECK:   %[[CT_ENTRY:[0-9]+]] = call token @llvm.experimental.convergence.entry()
diff --git a/clang/test/Options/enable_16bit_types_validation_spirv.hlsl b/clang/test/Options/enable_16bit_types_validation_spirv.hlsl
index aeb7a8369f4034..3e6734011c39ad 100644
--- a/clang/test/Options/enable_16bit_types_validation_spirv.hlsl
+++ b/clang/test/Options/enable_16bit_types_validation_spirv.hlsl
@@ -4,7 +4,7 @@
 // SPIRV: error: '-fnative-half-type' option requires target HLSL Version >= 2018, but HLSL Version is 'hlsl2016'
 
 // valid: "spirv-unknown-vulkan-library"
-// valid: define spir_func void @main() #0 {
+// valid: define spir_func void @{{.*}}main{{.*}}() #0 {
 
 [numthreads(1,1,1)]
 void main()

Copy link
Contributor

@bogner bogner left a comment

Choose a reason for hiding this comment

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

There is still logic elsewhere that sets the default entry point to "main", right? This looks right to me.

Copy link
Contributor

@s-perron s-perron left a comment

Choose a reason for hiding this comment

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

This fixes the problem in #108567

llvm-beanz and others added 2 commits September 30, 2024 16:09
Co-authored-by: Farzon Lotfi <1802579+farzonl@users.noreply.github.com>
Co-authored-by: Farzon Lotfi <1802579+farzonl@users.noreply.github.com>
@llvm-beanz
Copy link
Collaborator Author

There is still logic elsewhere that sets the default entry point to "main", right? This looks right to me.

Yes, and we apply the HLSLShader attribute to main and key generating the entry off that in clangCodeGen.

@llvm-beanz llvm-beanz merged commit b8b036a into llvm:main Oct 1, 2024
Sterling-Augustine pushed a commit to Sterling-Augustine/llvm-project that referenced this pull request Oct 3, 2024
HLSL doesn't distinguish `main` from any other function. It does treat
entry points special, but they're not required to be called `main` so we
have a different attribute annotation to mark them.

At the moment this change really just changes the mangling of functions
named `main` in the Itanium mangling.

Fixes llvm#110517

---------

Co-authored-by: Farzon Lotfi <1802579+farzonl@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category HLSL HLSL Language Support

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

[HLSL] main is just a function

6 participants