Skip to content

Commit

Permalink
emit header draft
Browse files Browse the repository at this point in the history
  • Loading branch information
pato-melowntech committed Apr 12, 2024
1 parent cc3e979 commit 53838a2
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 8 deletions.
1 change: 1 addition & 0 deletions slang.h
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,7 @@ extern "C"
NoHLSLBinding,
ValidateUniformity,
AllowGLSL,
EmitHeader,

// Internal

Expand Down
3 changes: 3 additions & 0 deletions source/slang/core.meta.slang
Original file line number Diff line number Diff line change
Expand Up @@ -2577,6 +2577,9 @@ attribute_syntax [AutoPyBindCUDA] : AutoPyBindCudaAttribute;
__attributeTarget(AggTypeDecl)
attribute_syntax [PyExport(name: String)] : PyExportAttribute;

__attributeTarget(DeclBase)
attribute_syntax [HeaderExport] : HeaderExportAttribute;

__attributeTarget(InterfaceDecl)
attribute_syntax [COM(guid: String)] : ComInterfaceAttribute;

Expand Down
5 changes: 5 additions & 0 deletions source/slang/slang-ast-modifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,11 @@ class PyExportAttribute : public Attribute
String name;
};

class HeaderExportAttribute : public Attribute
{
SLANG_AST_CLASS(HeaderExportAttribute)
};

class PreferRecomputeAttribute : public Attribute
{
SLANG_AST_CLASS(PreferRecomputeAttribute)
Expand Down
53 changes: 46 additions & 7 deletions source/slang/slang-emit-c-like.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,23 @@ String CLikeSourceEmitter::getName(IRInst* inst)
String name;
if(!m_mapInstToName.tryGetValue(inst, name))
{
name = generateName(inst);
// unmangle names, when emitting header
if (isEmmitingHeader()) {
if (auto nameHintDecor = inst->findDecoration<IRNameHintDecoration>())
{
StringBuilder sb;
for (auto c : nameHintDecor->getName()) {
if (c == '.')
sb.append('_');
else {
sb.append(c);
}
}
name = sb.produceString();
}
} else {
name = generateName(inst);
}
m_mapInstToName.add(inst, name);
}
return name;
Expand Down Expand Up @@ -3320,7 +3336,7 @@ void CLikeSourceEmitter::emitSimpleFuncImpl(IRFunc* func)
emitSemantics(func);

// TODO: encode declaration vs. definition
if(isDefinition(func))
if(!isEmmitingHeader() && isDefinition(func))
{
m_writer->emit("\n{\n");
m_writer->indent();
Expand Down Expand Up @@ -3450,6 +3466,11 @@ bool CLikeSourceEmitter::isTargetIntrinsic(IRInst* inst)
return findTargetIntrinsicDefinition(inst, intrinsicDef);
}

bool CLikeSourceEmitter::isEmmitingHeader() {
return getTargetProgram()->getOptionSet().getBoolOption(CompilerOptionName::EmitHeader);
}


bool shouldWrappInExternCBlock(IRFunc* func)
{
for (auto decor : func->getDecorations())
Expand All @@ -3466,6 +3487,11 @@ bool shouldWrappInExternCBlock(IRFunc* func)

void CLikeSourceEmitter::emitFunc(IRFunc* func)
{
// When emmiting header, skip if should not be emmited.
if (isEmmitingHeader() && !func->findDecoration<IRHeaderExportDecoration>()) {
return;
}

// Target-intrinsic functions should never be emitted
// even if they happen to have a body.
//
Expand Down Expand Up @@ -3513,6 +3539,13 @@ void CLikeSourceEmitter::emitFuncDecorationsImpl(IRFunc* func)

void CLikeSourceEmitter::emitStruct(IRStructType* structType)
{
// TODO: add similar functionality like markTypeForPyExport for [HeaderExport]

// When emmiting header, skip if should not be emmited.
if (isEmmitingHeader() && !structType->findDecoration<IRHeaderExportDecoration>()) {
return;
}

ensureTypePrelude(structType);

// If the selected `struct` type is actually an intrinsic
Expand Down Expand Up @@ -3907,11 +3940,17 @@ void CLikeSourceEmitter::emitGlobalVar(IRGlobalVar* varDecl)

m_writer->emit("\n");
emitType(varType, initFuncName);
m_writer->emit("()\n{\n");
m_writer->indent();
emitFunctionBody(varDecl);
m_writer->dedent();
m_writer->emit("}\n");

// When emmiting header, emit only declaration.
if (isEmmitingHeader()) {
m_writer->emit(";\n");
} else {
m_writer->emit("()\n{\n");
m_writer->indent();
emitFunctionBody(varDecl);
m_writer->dedent();
m_writer->emit("}\n");
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions source/slang/slang-emit-c-like.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ class CLikeSourceEmitter: public SourceEmitterBase

void emitParamType(IRType* type, String const& name) { emitParamTypeImpl(type, name); }

bool isEmmitingHeader();

void emitFuncDecl(IRFunc* func);
void emitFuncDecl(IRFunc* func, const String& name);

Expand Down
2 changes: 1 addition & 1 deletion source/slang/slang-emit-cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ void CPPSourceEmitter::emitSimpleFuncImpl(IRFunc* func)
emitSemantics(func);

// TODO: encode declaration vs. definition
if (isDefinition(func))
if (!isEmmitingHeader() && isDefinition(func))
{
m_writer->emit("\n{\n");
m_writer->indent();
Expand Down
1 change: 1 addition & 0 deletions source/slang/slang-ir-dll-export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct DllExportContext
dllExportDecoration->insertAtStart(wrapper);
builder.addNameHintDecoration(wrapper, dllExportDecoration->getFunctionName());
builder.addExternCppDecoration(wrapper, dllExportDecoration->getFunctionName());
builder.addHeaderExportDecoration(wrapper);
builder.addPublicDecoration(wrapper);
builder.addKeepAliveDecoration(wrapper);
builder.addHLSLExportDecoration(wrapper);
Expand Down
1 change: 1 addition & 0 deletions source/slang/slang-ir-inst-defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ INST(HighLevelDeclDecoration, highLevelDecl, 1, 0)
INST(CudaKernelBackwardDerivativeDecoration, CudaKernelBwdDiffRef, 0, 0)
INST(AutoPyBindExportInfoDecoration, PyBindExportFuncInfo, 0, 0)
INST(PyExportDecoration, PyExportDecoration, 0, 0)
INST(HeaderExportDecoration, HeaderExportDecoration, 0, 0)

/// Used to mark parameters that are moved from entry point parameters to global params as coming from the entry point.
INST(EntryPointParamDecoration, entryPointParam, 0, 0)
Expand Down
13 changes: 13 additions & 0 deletions source/slang/slang-ir-insts.h
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,14 @@ struct IRPyExportDecoration : IRDecoration
UnownedStringSlice getExportName() { return getExportNameOperand()->getStringSlice(); }
};

struct IRHeaderExportDecoration : IRDecoration
{
enum
{
kOp = kIROp_HeaderExportDecoration
};
IR_LEAF_ISA(HeaderExportDecoration)
};

struct IRKnownBuiltinDecoration : IRDecoration
{
Expand Down Expand Up @@ -4647,6 +4655,11 @@ struct IRBuilder
addDecoration(value, kIROp_PyExportDecoration, getStringValue(exportName));
}

void addHeaderExportDecoration(IRInst* value)
{
addDecoration(value, kIROp_HeaderExportDecoration);
}

void addCudaDeviceExportDecoration(IRInst* value, UnownedStringSlice const& functionName)
{
addDecoration(value, kIROp_CudaDeviceExportDecoration, getStringValue(functionName));
Expand Down
6 changes: 6 additions & 0 deletions source/slang/slang-lower-to-ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,7 @@ static void addLinkageDecoration(
else if (as<CudaKernelAttribute>(modifier))
{
builder->addCudaKernelDecoration(inst);
builder->addHeaderExportDecoration(inst);
builder->addExternCppDecoration(inst, decl->getName()->text.getUnownedSlice());
builder->addHLSLExportDecoration(inst);
builder->addKeepAliveDecoration(inst);
Expand All @@ -1408,6 +1409,11 @@ static void addLinkageDecoration(
: decl->getName()->text.getUnownedSlice());
builder->addHLSLExportDecoration(inst);
}
else if (as<HeaderExportAttribute>(modifier))
{
builder->addKeepAliveDecoration(inst);
builder->addHeaderExportDecoration(inst);
}
else if (auto knownBuiltinModifier = as<KnownBuiltinAttribute>(modifier))
{
// We add this to the internal instruction, like other name-like
Expand Down
2 changes: 2 additions & 0 deletions source/slang/slang-options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ void initCommandOptions(CommandOptions& options)
"except for parameters that has explicit bindings in the input source." },
{ OptionKind::ValidateUniformity, "-validate-uniformity", nullptr, "Perform uniformity validation analysis." },
{ OptionKind::AllowGLSL, "-allow-glsl", nullptr, "Enable GLSL as an input language." },
{ OptionKind::EmitHeader, "-emit-header", nullptr, "Emits a header instead of the code (for cpp or cuda target)." },
};
_addOptions(makeConstArrayView(experimentalOpts), options);

Expand Down Expand Up @@ -1664,6 +1665,7 @@ SlangResult OptionsParser::_parse(
case OptionKind::NoMangle:
case OptionKind::ValidateUniformity:
case OptionKind::AllowGLSL:
case OptionKind::EmitHeader:
case OptionKind::EmitIr:
case OptionKind::DumpIntermediates:
case OptionKind::DumpReproOnError:
Expand Down

0 comments on commit 53838a2

Please sign in to comment.