summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--source/slang/slang-emit-c-like.cpp79
-rw-r--r--source/slang/slang-ir-autodiff.cpp5
-rw-r--r--source/slang/slang-ir-util.cpp55
-rw-r--r--source/slang/slang-ir-util.h3
-rw-r--r--source/slang/slang-lower-to-ir.cpp38
5 files changed, 128 insertions, 52 deletions
diff --git a/source/slang/slang-emit-c-like.cpp b/source/slang/slang-emit-c-like.cpp
index 06132baa7..a1a9690f4 100644
--- a/source/slang/slang-emit-c-like.cpp
+++ b/source/slang/slang-emit-c-like.cpp
@@ -34,6 +34,82 @@ namespace Slang
bool isCPUTarget(TargetRequest* targetReq);
bool isCUDATarget(TargetRequest* targetReq);
+static bool _isHexDigit(char c)
+{
+ return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
+}
+
+static UnownedStringSlice _trimTrailingHexSuffix(const UnownedStringSlice& name)
+{
+ const Index len = name.getLength();
+ if (len >= 9 && name[len - 9] == '_')
+ {
+ bool allHex = true;
+ for (Index i = len - 8; i < len; ++i)
+ {
+ if (!_isHexDigit(name[i]))
+ {
+ allHex = false;
+ break;
+ }
+ }
+ if (allHex)
+ return name.head(len - 9);
+ }
+ return name;
+}
+
+static bool _sliceEndsWith(const UnownedStringSlice& value, const UnownedStringSlice& suffix)
+{
+ const Index suffixLen = suffix.getLength();
+ if (suffixLen == 0)
+ return true;
+ const Index valueLen = value.getLength();
+ if (valueLen < suffixLen)
+ return false;
+ for (Index i = 0; i < suffixLen; ++i)
+ {
+ if (value[valueLen - suffixLen + i] != suffix[i])
+ return false;
+ }
+ return true;
+}
+
+static String _ensureModuleSuffix(IRInst* inst, const UnownedStringSlice& hint)
+{
+ UnownedStringSlice trimmed = _trimTrailingHexSuffix(hint);
+ const bool hadHexSuffix = trimmed.getLength() != hint.getLength();
+
+ if (!hadHexSuffix && !shouldApplyModuleNonPublicSuffix(inst))
+ return String(hint);
+
+ IRModuleInst* moduleInst = nullptr;
+ if (inst)
+ {
+ if (auto module = inst->getModule())
+ moduleInst = module->getModuleInst();
+ }
+ if (!moduleInst)
+ return String(hint);
+
+ String suffix = getOrCreateModuleNonPublicSuffix(moduleInst);
+ if (!suffix.getLength())
+ return String(hint);
+
+ const UnownedStringSlice suffixSlice = suffix.getUnownedSlice();
+
+ if (_sliceEndsWith(hint, suffixSlice))
+ return String(hint);
+
+ if (_sliceEndsWith(trimmed, suffixSlice))
+ return String(trimmed);
+
+ StringBuilder sb;
+ sb.append(trimmed);
+ sb.append(suffixSlice);
+ return sb.produceString();
+}
+
struct CLikeSourceEmitter::ComputeEmitActionsContext
{
IRInst* moduleInst;
@@ -1250,7 +1326,8 @@ String CLikeSourceEmitter::generateName(IRInst* inst)
// to provide the basis for the actual name in the output code.
if (auto nameHintDecoration = inst->findDecoration<IRNameHintDecoration>())
{
- return _generateUniqueName(nameHintDecoration->getName());
+ String adjustedName = _ensureModuleSuffix(inst, nameHintDecoration->getName());
+ return _generateUniqueName(adjustedName.getUnownedSlice());
}
// If the instruction has a linkage decoration, just use that.
diff --git a/source/slang/slang-ir-autodiff.cpp b/source/slang/slang-ir-autodiff.cpp
index 298dccb1b..ef8e11395 100644
--- a/source/slang/slang-ir-autodiff.cpp
+++ b/source/slang/slang-ir-autodiff.cpp
@@ -841,10 +841,7 @@ IRInst* DifferentialPairTypeBuilder::_createDiffPairType(IRType* origBaseType, I
nameBuilder << "DiffPair_";
getTypeNameHint(nameBuilder, origBaseType);
String moduleSuffix = getOrCreateModuleNonPublicSuffix(sharedContext->moduleInst);
- if (moduleSuffix.getLength())
- {
- nameBuilder.append(moduleSuffix);
- }
+ nameBuilder.append(moduleSuffix);
builder.addNameHintDecoration(pairStructType, nameBuilder.toString().getUnownedSlice());
builder.createStructField(pairStructType, _getOrCreatePrimalStructKey(), origBaseType);
diff --git a/source/slang/slang-ir-util.cpp b/source/slang/slang-ir-util.cpp
index 1e00fcaf6..4c897063f 100644
--- a/source/slang/slang-ir-util.cpp
+++ b/source/slang/slang-ir-util.cpp
@@ -6,6 +6,7 @@
#include "slang-ir-insts.h"
#include <cstdint>
+#include <iostream>
#include <random>
namespace Slang
@@ -14,15 +15,12 @@ namespace Slang
static String _generateRandomSuffix()
{
std::random_device rd;
- uint32_t value = rd();
- if (value == 0)
- {
- value = 0x4AE33F2Eu; // arbitrary non-zero fallback
- }
+ uint32_t seed = rd();
+ std::cout << "seed: " << seed << std::endl;
const char kHexDigits[] = "0123456789abcdef";
char buffer[9];
- uint32_t temp = value;
+ uint32_t temp = seed;
for (int i = 7; i >= 0; --i)
{
buffer[i] = kHexDigits[temp & 0xF];
@@ -36,22 +34,59 @@ static String _generateRandomSuffix()
return sb.produceString();
}
-String getOrCreateModuleNonPublicSuffix(IRModuleInst* moduleInst)
+String getOrCreateModuleNonPublicSuffix(IRModuleInst* irmInst)
{
static Dictionary<IRModuleInst*, String> map;
- if (!moduleInst)
+ if (!irmInst)
return String();
String suffix;
- if (map.tryGetValue(moduleInst, suffix))
+ if (map.tryGetValue(irmInst, suffix))
return suffix;
suffix = _generateRandomSuffix();
- map[moduleInst] = suffix;
+ map[irmInst] = suffix;
+ std::cout << "generated suffix " << suffix.getBuffer() << std::endl;
return suffix;
}
+bool hasExplicitExportedLinkage(IRInst* inst)
+{
+ if (!inst)
+ return false;
+
+ return inst->findDecoration<IRPublicDecoration>() ||
+ inst->findDecoration<IRExportDecoration>() ||
+ inst->findDecoration<IRHLSLExportDecoration>() ||
+ inst->findDecoration<IRExternCppDecoration>() ||
+ inst->findDecoration<IRDllImportDecoration>() ||
+ inst->findDecoration<IRDllExportDecoration>() ||
+ inst->findDecoration<IRCudaDeviceExportDecoration>() ||
+ inst->findDecoration<IRCudaHostDecoration>() ||
+ inst->findDecoration<IRCudaKernelDecoration>() ||
+ inst->findDecoration<IRTorchEntryPointDecoration>() ||
+ inst->findDecoration<IRAutoPyBindCudaDecoration>() ||
+ inst->findDecoration<IRPyExportDecoration>();
+}
+
+bool shouldApplyModuleNonPublicSuffix(IRInst* inst)
+{
+ if (!inst)
+ return false;
+
+ if (hasExplicitExportedLinkage(inst))
+ return false;
+
+ if (as<IRGlobalValueWithCode>(inst) || as<IRStructType>(inst) || as<IRClassType>(inst) ||
+ as<IRInterfaceType>(inst))
+ {
+ return true;
+ }
+
+ return false;
+}
+
bool isPointerOfType(IRInst* type, IROp opCode)
{
if (auto ptrType = as<IRPtrTypeBase>(type))
diff --git a/source/slang/slang-ir-util.h b/source/slang/slang-ir-util.h
index d49381aa8..c52a1d820 100644
--- a/source/slang/slang-ir-util.h
+++ b/source/slang/slang-ir-util.h
@@ -36,6 +36,9 @@ public:
/// The suffix remains stable for the lifetime of the module instance.
String getOrCreateModuleNonPublicSuffix(IRModuleInst* moduleInst);
+bool hasExplicitExportedLinkage(IRInst* inst);
+bool shouldApplyModuleNonPublicSuffix(IRInst* inst);
+
struct DeduplicateContext
{
diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp
index d52b9bd04..1b08a0dff 100644
--- a/source/slang/slang-lower-to-ir.cpp
+++ b/source/slang/slang-lower-to-ir.cpp
@@ -2698,42 +2698,6 @@ static bool shouldApplyNonPublicSuffix(Decl* target)
return false;
}
-static bool hasExplicitExportedLinkage(IRInst* inst)
-{
- if (!inst)
- return false;
-
- return inst->findDecoration<IRPublicDecoration>() ||
- inst->findDecoration<IRExportDecoration>() ||
- inst->findDecoration<IRHLSLExportDecoration>() ||
- inst->findDecoration<IRExternCppDecoration>() ||
- inst->findDecoration<IRDllImportDecoration>() ||
- inst->findDecoration<IRDllExportDecoration>() ||
- inst->findDecoration<IRCudaDeviceExportDecoration>() ||
- inst->findDecoration<IRCudaHostDecoration>() ||
- inst->findDecoration<IRCudaKernelDecoration>() ||
- inst->findDecoration<IRTorchEntryPointDecoration>() ||
- inst->findDecoration<IRAutoPyBindCudaDecoration>() ||
- inst->findDecoration<IRPyExportDecoration>();
-}
-
-static bool shouldApplyNonPublicSuffix(IRInst* inst)
-{
- if (!inst)
- return false;
-
- if (hasExplicitExportedLinkage(inst))
- return false;
-
- if (as<IRGlobalValueWithCode>(inst) || as<IRStructType>(inst) || as<IRClassType>(inst) ||
- as<IRInterfaceType>(inst))
- {
- return true;
- }
-
- return false;
-}
-
static void appendSuffixIfNeeded(IRGenContext* context, IRInst* inst, Decl* decl, String& name)
{
bool needsSuffix = false;
@@ -2743,7 +2707,7 @@ static void appendSuffixIfNeeded(IRGenContext* context, IRInst* inst, Decl* decl
}
else
{
- needsSuffix = shouldApplyNonPublicSuffix(inst);
+ needsSuffix = shouldApplyModuleNonPublicSuffix(inst);
}
if (!needsSuffix)