summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMukund Keshava <mkeshava@nvidia.com>2025-05-15 14:45:43 +0530
committerGitHub <noreply@github.com>2025-05-15 14:45:43 +0530
commited837e205f3e67c4ae112f544cfe486ca3cc8455 (patch)
treeec67d243bcea5c613d24c898c301051117497577
parent49de1e8f60c698e9d524befacc988fb06574b234 (diff)
Rename 'main' on some backends (#7105)
* Rename 'main' on some backednds Fixes #5542 Some backends like cuda, metal, cpu do not allow 'main' as the entry point. This commit adds a new warning that is emitted when a program uses main as the entry point. In addition to emitting the warning, it internally renames the entry point to "main_". It also adds a test to check these for the three backends. * format code * move test to diagnostics * rename test to diagnostic * generate unique name --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
-rw-r--r--source/slang/slang-diagnostic-defs.h6
-rw-r--r--source/slang/slang-emit-c-like.cpp18
-rw-r--r--source/slang/slang-emit-c-like.h3
-rw-r--r--tests/diagnostics/entry-point-main-warning.slang14
4 files changed, 40 insertions, 1 deletions
diff --git a/source/slang/slang-diagnostic-defs.h b/source/slang/slang-diagnostic-defs.h
index dbd4da588..808d867b6 100644
--- a/source/slang/slang-diagnostic-defs.h
+++ b/source/slang/slang-diagnostic-defs.h
@@ -2765,6 +2765,12 @@ DIAGNOSTIC(
noBlocksOrIntrinsic,
"no blocks found for function definition, is there a '$0' intrinsic missing?")
+DIAGNOSTIC(
+ 40100,
+ Warning,
+ mainEntryPointRenamed,
+ "entry point '$0' is not allowed, and has been renamed to '$1'")
+
//
// Ray tracing
//
diff --git a/source/slang/slang-emit-c-like.cpp b/source/slang/slang-emit-c-like.cpp
index 141a843f2..b2dc9b014 100644
--- a/source/slang/slang-emit-c-like.cpp
+++ b/source/slang/slang-emit-c-like.cpp
@@ -1109,9 +1109,25 @@ void CLikeSourceEmitter::appendScrubbedName(const UnownedStringSlice& name, Stri
}
}
+inline String CLikeSourceEmitter::maybeMakeEntryPointNameValid(String name, DiagnosticSink* sink)
+{
+ if (isCPUTarget(getTargetReq()) || isCUDATarget(getTargetReq()) ||
+ isMetalTarget(getTargetReq()))
+ {
+ if (name == "main")
+ {
+ String newName = _generateUniqueName(name.getUnownedSlice());
+ sink->diagnose(SourceLoc(), Diagnostics::mainEntryPointRenamed, name, newName);
+ return newName;
+ }
+ }
+ return name;
+}
+
String CLikeSourceEmitter::generateEntryPointNameImpl(IREntryPointDecoration* entryPointDecor)
{
- return entryPointDecor->getName()->getStringSlice();
+ String name = entryPointDecor->getName()->getStringSlice();
+ return maybeMakeEntryPointNameValid(name, getSink());
}
String CLikeSourceEmitter::_generateUniqueName(const UnownedStringSlice& name)
diff --git a/source/slang/slang-emit-c-like.h b/source/slang/slang-emit-c-like.h
index f38158c0a..78793f655 100644
--- a/source/slang/slang-emit-c-like.h
+++ b/source/slang/slang-emit-c-like.h
@@ -739,6 +739,9 @@ protected:
Dictionary<const char*, IRStringLit*> m_builtinPreludes;
+ // Rename entry point if target doesn't allow the name (e.g., 'main')
+ virtual String maybeMakeEntryPointNameValid(String name, DiagnosticSink* sink);
+
// Indicates if we are emiting for DXC cooperative vector POC.
bool isCoopvecPoc = false;
};
diff --git a/tests/diagnostics/entry-point-main-warning.slang b/tests/diagnostics/entry-point-main-warning.slang
new file mode 100644
index 000000000..9d880dbb1
--- /dev/null
+++ b/tests/diagnostics/entry-point-main-warning.slang
@@ -0,0 +1,14 @@
+// Test to check if we emit a warning and rename main to main_ on CUDA, CPU, metal backends
+
+//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): -entry main -target cuda
+//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): -entry main -target metal
+//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): -entry main -target cpp
+[shader("compute")]
+[numthreads(1,1,1)]
+void main(RWBuffer<float> output)
+{
+ output[0] = 1.0f;
+}
+
+// CHECK: warning 40100: entry point 'main' is not allowed, and has been renamed to 'main_0'
+// CHECK: void main_ \ No newline at end of file