Skip to content

Commit 9f29155

Browse files
committed
[irstate] add addrspace to string literals
This is required for Vulkan support where these live in a different address space
1 parent a89f095 commit 9f29155

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

gen/irstate.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,11 @@ template <typename F>
191191
LLGlobalVariable *
192192
getCachedStringLiteralImpl(llvm::Module &module,
193193
llvm::StringMap<LLGlobalVariable *> &cache,
194-
llvm::StringRef key, F initFactory) {
194+
llvm::StringRef key,
195+
#if LDC_LLVM_VER >= 1700
196+
std::optional< unsigned > addrspace,
197+
#endif
198+
F initFactory) {
195199
auto iter = cache.find(key);
196200
if (iter != cache.end()) {
197201
return iter->second;
@@ -201,7 +205,12 @@ getCachedStringLiteralImpl(llvm::Module &module,
201205

202206
auto gvar =
203207
new LLGlobalVariable(module, constant->getType(), true,
204-
LLGlobalValue::PrivateLinkage, constant, ".str");
208+
LLGlobalValue::PrivateLinkage, constant,
209+
".str", nullptr, llvm::GlobalValue::NotThreadLocal
210+
#if LDC_LLVM_VER >= 1700
211+
, addrspace
212+
#endif
213+
);
205214
gvar->setUnnamedAddr(LLGlobalValue::UnnamedAddr::Global);
206215

207216
cache[key] = gvar;
@@ -241,14 +250,28 @@ LLGlobalVariable *IRState::getCachedStringLiteral(StringExp *se) {
241250
const llvm::StringRef key(reinterpret_cast<const char *>(keyData.ptr),
242251
keyData.length);
243252

244-
return getCachedStringLiteralImpl(module, *cache, key, [se]() {
253+
return getCachedStringLiteralImpl(module, *cache, key,
254+
#if LDC_LLVM_VER >= 1700
255+
std::nullopt,
256+
#endif
257+
[se]() {
245258
// null-terminate
246259
return buildStringLiteralConstant(se, se->len + 1);
247260
});
248261
}
249262

250-
LLGlobalVariable *IRState::getCachedStringLiteral(llvm::StringRef s) {
251-
return getCachedStringLiteralImpl(module, cachedStringLiterals, s, [&]() {
263+
LLGlobalVariable *IRState::getCachedStringLiteral(llvm::StringRef s
264+
#if LDC_LLVM_VER >= 1700
265+
,std::optional< unsigned > addrspace
266+
#endif
267+
) {
268+
return getCachedStringLiteralImpl(module,
269+
cachedStringLiterals,
270+
s,
271+
#if LDC_LLVM_VER >= 1700
272+
addrspace,
273+
#endif
274+
[&]() {
252275
return llvm::ConstantDataArray::getString(context(), s, true);
253276
});
254277
}

gen/irstate.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <set>
2828
#include <sstream>
2929
#include <vector>
30+
#include <optional>
3031

3132
namespace llvm {
3233
class LLVMContext;
@@ -248,7 +249,11 @@ struct IRState {
248249
// calls with a StringExp with matching data will return the same variable.
249250
// Exception: ulong[]-typed hex strings (not null-terminated either).
250251
llvm::GlobalVariable *getCachedStringLiteral(StringExp *se);
251-
llvm::GlobalVariable *getCachedStringLiteral(llvm::StringRef s);
252+
llvm::GlobalVariable *getCachedStringLiteral(llvm::StringRef s
253+
#if LDC_LLVM_VER >= 1600
254+
,std::optional< unsigned > = std::nullopt
255+
#endif
256+
);
252257

253258
// List of functions with cpu or features attributes overriden by user
254259
std::vector<IrFunction *> targetCpuOrFeaturesOverridden;

0 commit comments

Comments
 (0)