Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions lib/translator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<id>" 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<id>" 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));
}
});
}
Expand Down
3 changes: 3 additions & 0 deletions lib/translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
15 changes: 12 additions & 3 deletions lib/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,9 @@ std::unordered_set<std::string> 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') ||
Expand All @@ -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++) {
Expand Down
44 changes: 44 additions & 0 deletions tests/cases/linkage_name_sanitize.spt
Original file line number Diff line number Diff line change
@@ -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(
Loading