diff --git a/lib/translator.cpp b/lib/translator.cpp index 5e9a2af..08cc3e1 100644 --- a/lib/translator.cpp +++ b/lib/translator.cpp @@ -674,7 +674,9 @@ bool translator_impl::translate_annotations() { void translator_impl::assign_names() { // Names fixed by the module's interface first, so nothing else can take - // them. Entry points are named by their OpEntryPoint. + // them. Entry points are named by their OpEntryPoint. Linkage names that + // aren't valid identifiers (e.g. the "spirv.llvm_fshl_i64" helpers of LLVM's + // SPIR-V back-end) can't be kept, and are sanitized below like OpNames. auto fix = [this](uint32_t id, const std::string &name) { if (!m_names.count(id)) { m_name_allocator.reserve(name); @@ -684,25 +686,38 @@ void translator_impl::assign_names() { for (auto &ep : m_entry_points) { fix(ep.first, ep.second); } - for (auto &exp : m_exports) { - fix(exp.first, exp.second); - } - for (auto &imp : m_imports) { - fix(imp.first, imp.second); + for (auto *linkage : {&m_exports, &m_imports}) { + for (auto &id_name : *linkage) { + if (name_allocator::is_identifier(id_name.second)) { + fix(id_name.first, id_name.second); + } + } } - // Then the OpNames, before the made-up "v" names so that those yield on - // a clash. Both in module order, for stable output. + // Then the remaining linkage names and the OpNames, before the made-up + // "v" names so that those yield on a clash. Both in module order, for + // stable output. + auto hint_for = [this](uint32_t id) -> const std::string * { + for (auto *linkage : {&m_exports, &m_imports}) { + auto it = linkage->find(id); + if (it != linkage->end()) { + return &it->second; + } + } + auto it = m_debug_names.find(id); + return it != m_debug_names.end() && !it->second.empty() ? &it->second + : nullptr; + }; for (bool named : {true, false}) { - m_ir->module()->ForEachInst([this, named](const Instruction *inst) { + m_ir->module()->ForEachInst([this, named, + &hint_for](const Instruction *inst) { auto id = inst->result_id(); if (id == 0 || m_names.count(id)) { return; } - auto it = m_debug_names.find(id); - bool has_name = it != m_debug_names.end() && !it->second.empty(); - if (has_name == named) { - m_names[id] = m_name_allocator.allocate( - named ? it->second : "v" + std::to_string(id)); + auto hint = hint_for(id); + if ((hint != nullptr) == named) { + m_names[id] = + m_name_allocator.allocate(named ? *hint : "v" + std::to_string(id)); } }); } diff --git a/lib/translator.h b/lib/translator.h index 584fd2c..ee14664 100644 --- a/lib/translator.h +++ b/lib/translator.h @@ -60,6 +60,9 @@ class name_allocator { // points, linkage names) that can't be changed. void reserve(const std::string &name) { m_used.insert(name); } + // Whether `name` is spelled like a C identifier. + static bool is_identifier(const std::string &name); + // A fresh identifier based on `hint`. std::string allocate(const std::string &hint); diff --git a/lib/utils.cpp b/lib/utils.cpp index f5d2c22..ff71a33 100644 --- a/lib/utils.cpp +++ b/lib/utils.cpp @@ -374,9 +374,9 @@ std::unordered_set gReservedIdentifiers = { "pipe", }; -std::string name_allocator::allocate(const std::string &hint) { - // Map anything that isn't a letter, digit or underscore to '_' (e.g. Julia - // names like "a::CLDeviceArray"), and avoid a leading digit. +// Map anything that isn't a letter, digit or underscore to '_' (e.g. Julia +// names like "a::CLDeviceArray"), and avoid a leading digit. +static std::string sanitize_identifier(const std::string &hint) { std::string base = hint.empty() ? "v" : hint; for (auto &ch : base) { bool valid = (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || @@ -388,6 +388,15 @@ std::string name_allocator::allocate(const std::string &hint) { if (base[0] >= '0' && base[0] <= '9') { base = "_" + base; } + return base; +} + +bool name_allocator::is_identifier(const std::string &name) { + return sanitize_identifier(name) == name; +} + +std::string name_allocator::allocate(const std::string &hint) { + auto base = sanitize_identifier(hint); auto name = base; for (unsigned n = 1; m_used.count(name) || gReservedIdentifiers.count(name); n++) { diff --git a/tests/cases/linkage_name_sanitize.spt b/tests/cases/linkage_name_sanitize.spt new file mode 100644 index 0000000..e8a52e0 --- /dev/null +++ b/tests/cases/linkage_name_sanitize.spt @@ -0,0 +1,44 @@ +; RUN: %spirv2clc --asm %s | %clang -c -cl-std=CL1.2 -x cl - -o /dev/null +; RUN: %spirv2clc --asm %s | %FileCheck %s +; Linkage names that aren't valid C identifiers, like the "spirv.llvm_fshl_i64" +; helpers LLVM's SPIR-V back-end exports, can't be kept and must be sanitized, +; without taking a valid linkage name that is kept verbatim. + OpCapability Kernel + OpCapability Addresses + OpCapability Linkage + OpCapability Int64 + OpDecorate %helper LinkageAttributes "spirv.llvm_fshl_i64" Export + OpDecorate %other LinkageAttributes "spirv_llvm_fshl_i64" Export + OpMemoryModel Physical64 OpenCL + OpEntryPoint Kernel %k "k" + %void = OpTypeVoid + %ulong = OpTypeInt 64 0 + %pulong = OpTypePointer CrossWorkgroup %ulong + %ulong_8 = OpConstant %ulong 8 + %helpty = OpTypeFunction %ulong %ulong + %kty = OpTypeFunction %void %pulong + %helper = OpFunction %ulong None %helpty + %x = OpFunctionParameter %ulong + %hb = OpLabel + %y = OpShiftLeftLogical %ulong %x %ulong_8 + OpReturnValue %y + OpFunctionEnd + %other = OpFunction %ulong None %helpty + %x2 = OpFunctionParameter %ulong + %ob = OpLabel + %y2 = OpShiftRightLogical %ulong %x2 %ulong_8 + OpReturnValue %y2 + OpFunctionEnd + %k = OpFunction %void None %kty + %p = OpFunctionParameter %pulong + %kb = OpLabel + %v = OpLoad %ulong %p + %r = OpFunctionCall %ulong %helper %v + %r2 = OpFunctionCall %ulong %other %r + OpStore %p %r2 + OpReturn + OpFunctionEnd +; CHECK: ulong spirv_llvm_fshl_i64_1( +; CHECK: ulong spirv_llvm_fshl_i64( +; CHECK: = spirv_llvm_fshl_i64_1( +; CHECK: = spirv_llvm_fshl_i64(