Skip to content

Commit 9a09e2e

Browse files
authored
Feature/decor entry point name (shader-slang#1073)
* Use name hint on EntryPoint naming. * Placed the entry point name on the EntryPointDecoration.
1 parent 0b61643 commit 9a09e2e

7 files changed

+39
-44
lines changed

source/slang/slang-emit-c-like.cpp

+21-30
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,25 @@ String CLikeSourceEmitter::generateName(IRInst* inst)
600600
return String(intrinsicDecoration->getDefinition());
601601
}
602602

603+
auto entryPointDecor = inst->findDecoration<IREntryPointDecoration>();
604+
if (entryPointDecor)
605+
{
606+
if (getSourceStyle() == SourceStyle::GLSL)
607+
{
608+
// GLSL will always need to use `main` as the
609+
// name for an entry-point function, but other
610+
// targets should try to use the original name.
611+
//
612+
// TODO: always use the original name, and
613+
// use the appropriate options for glslang to
614+
// make it support a non-`main` name.
615+
//
616+
return "main";
617+
}
618+
619+
return entryPointDecor->getName()->getStringSlice();
620+
}
621+
603622
// If we have a name hint on the instruction, then we will try to use that
604623
// to provide the actual name in the output code.
605624
//
@@ -2448,34 +2467,6 @@ bool CLikeSourceEmitter::isDefinition(IRFunc* func)
24482467
return func->getFirstBlock() != nullptr;
24492468
}
24502469

2451-
String CLikeSourceEmitter::getFuncName(IRFunc* func)
2452-
{
2453-
if (auto entryPointLayout = asEntryPoint(func))
2454-
{
2455-
// GLSL will always need to use `main` as the
2456-
// name for an entry-point function, but other
2457-
// targets should try to use the original name.
2458-
//
2459-
// TODO: always use the original name, and
2460-
// use the appropriate options for glslang to
2461-
// make it support a non-`main` name.
2462-
//
2463-
if (getSourceStyle() != SourceStyle::GLSL)
2464-
{
2465-
return getText(entryPointLayout->getFuncDecl()->getName());
2466-
}
2467-
2468-
//
2469-
2470-
return "main";
2471-
}
2472-
else
2473-
{
2474-
return getName(func);
2475-
}
2476-
}
2477-
2478-
24792470
void CLikeSourceEmitter::emitEntryPointAttributes(IRFunc* irFunc, IREntryPointDecoration* entryPointDecor)
24802471
{
24812472
emitEntryPointAttributesImpl(irFunc, entryPointDecor);
@@ -2549,7 +2540,7 @@ void CLikeSourceEmitter::emitSimpleFuncImpl(IRFunc* func)
25492540
emitEntryPointAttributes(func, entryPointDecor);
25502541
}
25512542

2552-
auto name = getFuncName(func);
2543+
auto name = getName(func);
25532544

25542545
emitType(resultType, name);
25552546

@@ -2657,7 +2648,7 @@ void CLikeSourceEmitter::emitFuncDecl(IRFunc* func)
26572648
auto funcType = func->getDataType();
26582649
auto resultType = func->getResultType();
26592650

2660-
auto name = getFuncName(func);
2651+
auto name = getName(func);
26612652

26622653
emitType(resultType, name);
26632654

source/slang/slang-emit-c-like.h

-2
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,6 @@ class CLikeSourceEmitter: public RefObject
239239
// Is an IR function a definition? (otherwise it is a declaration)
240240
bool isDefinition(IRFunc* func);
241241

242-
String getFuncName(IRFunc* func);
243-
244242
void emitEntryPointAttributes(IRFunc* irFunc, IREntryPointDecoration* entryPointDecor);
245243

246244
void emitPhiVarDecls(IRFunc* func);

source/slang/slang-emit-cpp.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -1939,15 +1939,14 @@ void CPPSourceEmitter::emitSimpleFuncImpl(IRFunc* func)
19391939
{
19401940
auto resultType = func->getResultType();
19411941

1942-
auto name = getFuncName(func);
1942+
auto name = getName(func);
19431943

19441944
// Deal with decorations that need
19451945
// to be emitted as attributes
19461946

19471947
// We are going to ignore the parameters passed and just pass in the Context
19481948

1949-
auto entryPointLayout = asEntryPoint(func);
1950-
if (entryPointLayout)
1949+
if (IREntryPointDecoration* entryPointDecor = func->findDecoration<IREntryPointDecoration>())
19511950
{
19521951
StringBuilder prefixName;
19531952
prefixName << "_" << name;
@@ -2809,16 +2808,17 @@ void CPPSourceEmitter::emitModuleImpl(IRModule* module)
28092808
{
28102809
IRFunc* func = as<IRFunc>(action.inst);
28112810

2812-
auto entryPointLayout = asEntryPoint(func);
2813-
if (entryPointLayout && entryPointLayout->profile.GetStage() == Stage::Compute)
2811+
IREntryPointDecoration* entryPointDecor = func->findDecoration<IREntryPointDecoration>();
2812+
2813+
if (entryPointDecor && entryPointDecor->getProfile().GetStage() == Stage::Compute)
28142814
{
28152815
// https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sv-dispatchthreadid
28162816
// SV_DispatchThreadID is the sum of SV_GroupID * numthreads and GroupThreadID.
28172817

28182818
Int groupThreadSize[kThreadGroupAxisCount];
28192819
getComputeThreadGroupSize(func, groupThreadSize);
28202820

2821-
String funcName = getFuncName(func);
2821+
String funcName = getName(func);
28222822

28232823
{
28242824
StringBuilder builder;
@@ -2852,7 +2852,7 @@ void CPPSourceEmitter::emitModuleImpl(IRModule* module)
28522852
// Emit the group version which runs for all elements in *single* thread group
28532853
{
28542854
StringBuilder builder;
2855-
builder << getFuncName(func);
2855+
builder << getName(func);
28562856
builder << "_Group";
28572857

28582858
String groupFuncName = builder;

source/slang/slang-ir-inst-defs.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ INST(HighLevelDeclDecoration, highLevelDecl, 1, 0)
434434
INST(StreamOutputTypeDecoration, streamOutputTypeDecoration, 1, 0)
435435

436436
/// An `[entryPoint]` decoration marks a function that represents a shader entry point
437-
INST(EntryPointDecoration, entryPoint, 1, 0)
437+
INST(EntryPointDecoration, entryPoint, 2, 0)
438438

439439
/// Used to mark parameters that are moved from entry point parameters to global params as coming from the entry point.
440440
INST(EntryPointParamDecoration, entryPointParam, 0, 0)

source/slang/slang-ir-insts.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ struct IREntryPointDecoration : IRDecoration
294294

295295
IRIntLit* getProfileInst() { return cast<IRIntLit>(getOperand(0)); }
296296
Profile getProfile() { return Profile(Profile::RawVal(GetIntVal(getProfileInst()))); }
297+
298+
IRStringLit* getName() { return cast<IRStringLit>(getOperand(1)); }
297299
};
298300

299301
struct IRGeometryInputPrimitiveTypeDecoration: IRDecoration
@@ -1400,9 +1402,9 @@ struct IRBuilder
14001402
addDecoration(value, kIROp_ExportDecoration, getStringValue(mangledName));
14011403
}
14021404

1403-
void addEntryPointDecoration(IRInst* value, Profile profile)
1405+
void addEntryPointDecoration(IRInst* value, Profile profile, UnownedStringSlice const& name)
14041406
{
1405-
addDecoration(value, kIROp_EntryPointDecoration, getIntValue(getIntType(), profile.raw));
1407+
addDecoration(value, kIROp_EntryPointDecoration, getIntValue(getIntType(), profile.raw), getStringValue(name));
14061408
}
14071409

14081410
void addKeepAliveDecoration(IRInst* value)

source/slang/slang-lower-to-ir.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -6338,7 +6338,11 @@ static void lowerFrontEndEntryPointToIR(
63386338
{
63396339
instToDecorate = findGenericReturnVal(irGeneric);
63406340
}
6341-
builder->addEntryPointDecoration(instToDecorate, entryPoint->getProfile());
6341+
6342+
{
6343+
Name* entryPointName = entryPoint->getFuncDecl()->getName();
6344+
builder->addEntryPointDecoration(instToDecorate, entryPoint->getProfile(), entryPointName->text.getUnownedSlice());
6345+
}
63426346

63436347
// Go through the entry point parameters creating decorations from layout as appropriate
63446348
{

tests/ir/string-literal.slang.expected

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
result code = 0
22
standard error = {
3-
[entryPoint(6)]
3+
[entryPoint(6, "main")]
44
[numThreads(1, 1, 1)]
55
[export("_S3tu04mainp1puV")]
66
[nameHint("main")]

0 commit comments

Comments
 (0)