From 5b2eb06816521cc0fcfe03258452560bd200002d Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 21 Sep 2023 14:00:48 -0700 Subject: Various slangpy fixes. (#3227) * Make dynamic cast transparent through `IRAttributedType`. * Add [CUDAXxx] variant of attributes. * Support marshaling of vector types. * Wrap cuda kernels in `extern "C"` block. --------- Co-authored-by: Yong He --- source/slang/slang-emit-c-like.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'source/slang/slang-emit-c-like.cpp') diff --git a/source/slang/slang-emit-c-like.cpp b/source/slang/slang-emit-c-like.cpp index a74459954..d26893987 100644 --- a/source/slang/slang-emit-c-like.cpp +++ b/source/slang/slang-emit-c-like.cpp @@ -3321,6 +3321,20 @@ bool CLikeSourceEmitter::isTargetIntrinsic(IRInst* inst) return findTargetIntrinsicDefinition(inst, intrinsicDef); } +bool shouldWrappInExternCBlock(IRFunc* func) +{ + for (auto decor : func->getDecorations()) + { + switch (decor->getOp()) + { + case kIROp_ExternCDecoration: + case kIROp_CudaKernelDecoration: + return true; + } + } + return false; +} + void CLikeSourceEmitter::emitFunc(IRFunc* func) { // Target-intrinsic functions should never be emitted @@ -3329,6 +3343,15 @@ void CLikeSourceEmitter::emitFunc(IRFunc* func) if (isTargetIntrinsic(func)) return; + bool shouldCloseExternCBlock = shouldWrappInExternCBlock(func); + if (shouldCloseExternCBlock) + { + // If this is a C++ `extern "C"` function, then we need to emit + // it as a C function, since that is what the C++ compiler will + // expect. + // + m_writer->emit("extern \"C\" {\n"); + } if(!isDefinition(func)) { @@ -3345,6 +3368,10 @@ void CLikeSourceEmitter::emitFunc(IRFunc* func) // emitSimpleFunc(func); } + if (shouldCloseExternCBlock) + { + m_writer->emit("}\n"); + } } void CLikeSourceEmitter::emitFuncDecorationsImpl(IRFunc* func) -- cgit v1.2.3