Skip to content

Commit a427570

Browse files
authored
Reimplement globalstring to provide addrspace control. (#479)
1 parent a202084 commit a427570

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

src/core/value/constant.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -599,12 +599,12 @@ const_fcmp(Predicate::API.LLVMRealPredicate, lhs::Constant, rhs::Constant) =
599599
const_shl(lhs::Constant, rhs::Constant) =
600600
Value(API.LLVMConstShl(lhs, rhs))
601601

602-
function const_gep(val::Constant, Ty::LLVMType, Indices::Vector{<:Constant})
603-
Value(API.LLVMConstGEP2(val, Ty, Indices, length(Indices)))
602+
function const_gep(Ty::LLVMType, val::Constant, Indices::Vector{<:Constant})
603+
Value(API.LLVMConstGEP2(Ty, val, Indices, length(Indices)))
604604
end
605605

606-
function const_inbounds_gep(val::Constant, Ty::LLVMType, Indices::Vector{<:Constant})
607-
Value(API.LLVMConstInBoundsGEP2(val, Ty, Indices, length(Indices)))
606+
function const_inbounds_gep(Ty::LLVMType, val::Constant, Indices::Vector{<:Constant})
607+
Value(API.LLVMConstInBoundsGEP2(Ty, val, Indices, length(Indices)))
608608
end
609609

610610
const_trunc(val::Constant, ToType::LLVMType) =

src/irbuilder.jl

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -518,11 +518,41 @@ not!(builder::IRBuilder, V::Value, Name::String="") =
518518

519519
# other build methods
520520

521-
globalstring!(builder::IRBuilder, Str::String, Name::String="") =
522-
Value(API.LLVMBuildGlobalString(builder, Str, Name))
521+
#globalstring!(builder::IRBuilder, Str::String, Name::String="") =
522+
# Value(API.LLVMBuildGlobalString(builder, Str, Name))
523+
524+
# re-implementation for flexibility (exposing addrspace, add_null)
525+
function globalstring!(mod::LLVM.Module, str::String, name::String="";
526+
addrspace::Integer=0, add_null::Bool=true)
527+
bytes = Vector{UInt8}(str)
528+
if add_null
529+
push!(bytes, 0x00)
530+
end
531+
constant = ConstantDataArray(bytes)
532+
533+
gv = GlobalVariable(mod, value_type(constant), name, addrspace)
534+
alignment!(gv, 1)
535+
unnamed_addr!(gv, true)
536+
initializer!(gv, constant)
537+
constant!(gv, true)
538+
linkage!(gv, LLVM.API.LLVMPrivateLinkage)
523539

524-
globalstring_ptr!(builder::IRBuilder, Str::String, Name::String="") =
525-
Value(API.LLVMBuildGlobalStringPtr(builder, Str, Name))
540+
return gv
541+
end
542+
function globalstring!(builder::IRBuilder, args...; kwargs...)
543+
mod = parent(parent(position(builder)))
544+
globalstring!(mod, args...; kwargs...)
545+
end
546+
547+
#globalstring_ptr!(builder::IRBuilder, Str::String, Name::String="") =
548+
# Value(API.LLVMBuildGlobalStringPtr(builder, Str, Name))
549+
550+
function globalstring_ptr!(args...; kwargs...)
551+
gv = globalstring!(args...; kwargs...)
552+
zero = LLVM.ConstantInt(LLVM.IntType(32), 0)
553+
indices = [zero, zero]
554+
const_inbounds_gep(global_value_type(gv), gv, indices)
555+
end
526556

527557
isnull!(builder::IRBuilder, Val::Value, Name::String="") =
528558
Value(API.LLVMBuildIsNull(builder, Val, Name))

test/instructions_tests.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,16 @@
389389
strinst = globalstring!(builder, "foobar")
390390
@check_ir strinst "private unnamed_addr constant [7 x i8] c\"foobar\\00\""
391391

392+
str2inst = globalstring!(builder, "foobar"; addrspace=2, add_null=false)
393+
@check_ir str2inst "private unnamed_addr addrspace(2) constant [6 x i8] c\"foobar\""
394+
392395
strptrinst = globalstring_ptr!(builder, "foobar")
393396
if supports_typed_pointers(ctx)
394-
@check_ir strptrinst "i8* getelementptr inbounds ([7 x i8], [7 x i8]* @1, i32 0, i32 0)"
397+
@check_ir strptrinst "i8* getelementptr inbounds ([7 x i8], [7 x i8]* @2, i32 0, i32 0)"
395398
elseif LLVM.version() < v"15"
396399
# globalstring_ptr! returns a i8* ptr instead of a ptr to an i8 array.
397400
# that difference is moot when we have opaque pointers...
398-
@check_ir strptrinst "ptr getelementptr inbounds ([7 x i8], ptr @1, i32 0, i32 0)"
401+
@check_ir strptrinst "ptr getelementptr inbounds ([7 x i8], ptr @2, i32 0, i32 0)"
399402
else
400403
# ... so it is folded away now.
401404
@check_ir strptrinst "private unnamed_addr constant [7 x i8] c\"foobar\\00\""

0 commit comments

Comments
 (0)