Skip to content

Emit global strings in the correct address space. #516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 17, 2025

Conversation

maleadt
Copy link
Collaborator

@maleadt maleadt commented Jun 16, 2025

Copy link
Contributor

Your PR requires formatting changes to meet the project's style guidelines.
Please consider running Runic (git runic master) to apply these changes.

Click here to view the suggested changes.
diff --git a/lib/15/libLLVM_extra.jl b/lib/15/libLLVM_extra.jl
index 8a330824..8a3ceb29 100644
--- a/lib/15/libLLVM_extra.jl
+++ b/lib/15/libLLVM_extra.jl
@@ -402,6 +402,6 @@ function LLVMRunJuliaPassesOnFunction(F, Passes, TM, Options, Extensions)
 end
 
 function LLVMGlobalsAddressSpace(TD)
-    ccall((:LLVMGlobalsAddressSpace, libLLVMExtra), Cuint, (LLVMTargetDataRef,), TD)
+    return ccall((:LLVMGlobalsAddressSpace, libLLVMExtra), Cuint, (LLVMTargetDataRef,), TD)
 end
 
diff --git a/lib/16/libLLVM_extra.jl b/lib/16/libLLVM_extra.jl
index 8a330824..8a3ceb29 100644
--- a/lib/16/libLLVM_extra.jl
+++ b/lib/16/libLLVM_extra.jl
@@ -402,6 +402,6 @@ function LLVMRunJuliaPassesOnFunction(F, Passes, TM, Options, Extensions)
 end
 
 function LLVMGlobalsAddressSpace(TD)
-    ccall((:LLVMGlobalsAddressSpace, libLLVMExtra), Cuint, (LLVMTargetDataRef,), TD)
+    return ccall((:LLVMGlobalsAddressSpace, libLLVMExtra), Cuint, (LLVMTargetDataRef,), TD)
 end
 
diff --git a/lib/17/libLLVM_extra.jl b/lib/17/libLLVM_extra.jl
index b7ef156c..4d36910a 100644
--- a/lib/17/libLLVM_extra.jl
+++ b/lib/17/libLLVM_extra.jl
@@ -362,6 +362,6 @@ function LLVMRunJuliaPassesOnFunction(F, Passes, TM, Options, Extensions)
 end
 
 function LLVMGlobalsAddressSpace(TD)
-    ccall((:LLVMGlobalsAddressSpace, libLLVMExtra), Cuint, (LLVMTargetDataRef,), TD)
+    return ccall((:LLVMGlobalsAddressSpace, libLLVMExtra), Cuint, (LLVMTargetDataRef,), TD)
 end
 
diff --git a/lib/18/libLLVM_extra.jl b/lib/18/libLLVM_extra.jl
index 0f7f3edf..8f2082fc 100644
--- a/lib/18/libLLVM_extra.jl
+++ b/lib/18/libLLVM_extra.jl
@@ -296,6 +296,6 @@ function LLVMRunJuliaPassesOnFunction(F, Passes, TM, Options, Extensions)
 end
 
 function LLVMGlobalsAddressSpace(TD)
-    ccall((:LLVMGlobalsAddressSpace, libLLVMExtra), Cuint, (LLVMTargetDataRef,), TD)
+    return ccall((:LLVMGlobalsAddressSpace, libLLVMExtra), Cuint, (LLVMTargetDataRef,), TD)
 end
 
diff --git a/lib/19/libLLVM_extra.jl b/lib/19/libLLVM_extra.jl
index 0f7f3edf..8f2082fc 100644
--- a/lib/19/libLLVM_extra.jl
+++ b/lib/19/libLLVM_extra.jl
@@ -296,6 +296,6 @@ function LLVMRunJuliaPassesOnFunction(F, Passes, TM, Options, Extensions)
 end
 
 function LLVMGlobalsAddressSpace(TD)
-    ccall((:LLVMGlobalsAddressSpace, libLLVMExtra), Cuint, (LLVMTargetDataRef,), TD)
+    return ccall((:LLVMGlobalsAddressSpace, libLLVMExtra), Cuint, (LLVMTargetDataRef,), TD)
 end
 
diff --git a/lib/20/libLLVM_extra.jl b/lib/20/libLLVM_extra.jl
index b9b25a7a..d69b14c9 100644
--- a/lib/20/libLLVM_extra.jl
+++ b/lib/20/libLLVM_extra.jl
@@ -248,6 +248,6 @@ function LLVMRunJuliaPassesOnFunction(F, Passes, TM, Options, Extensions)
 end
 
 function LLVMGlobalsAddressSpace(TD)
-    ccall((:LLVMGlobalsAddressSpace, libLLVMExtra), Cuint, (LLVMTargetDataRef,), TD)
+    return ccall((:LLVMGlobalsAddressSpace, libLLVMExtra), Cuint, (LLVMTargetDataRef,), TD)
 end
 
diff --git a/src/datalayout.jl b/src/datalayout.jl
index dc8a93fa..cebe46f6 100644
--- a/src/datalayout.jl
+++ b/src/datalayout.jl
@@ -1,7 +1,7 @@
 ## data layout
 
 export DataLayout, dispose,
-       byteorder, pointersize, intptr, globals_addrspace,
+    byteorder, pointersize, intptr, globals_addrspace,
        sizeof, storage_size, abi_size,
        abi_alignment, frame_alignment, preferred_alignment,
        element_at, offsetof
@@ -57,7 +57,7 @@ Base.string(dl::DataLayout) =
     unsafe_message(API.LLVMCopyStringRepOfTargetData(dl))
 
 function Base.show(io::IO, dl::DataLayout)
-    @printf(io, "DataLayout(%s)", string(dl))
+    return @printf(io, "DataLayout(%s)", string(dl))
 end
 
 """
@@ -72,7 +72,7 @@ byteorder(dl::DataLayout) = API.LLVMByteOrder(dl)
 
 Get the pointer size of the target data layout.
 """
-pointersize(dl::DataLayout, addrspace::Integer=0) =
+pointersize(dl::DataLayout, addrspace::Integer = 0) =
     API.LLVMPointerSizeForAS(dl, addrspace)
 
 """
@@ -80,7 +80,7 @@ pointersize(dl::DataLayout, addrspace::Integer=0) =
 
 Get the integer type that is the same size as a pointer for the target data layout.
 """
-intptr(dl::DataLayout, addrspace::Integer=0) =
+intptr(dl::DataLayout, addrspace::Integer = 0) =
     IntegerType(API.LLVMIntPtrTypeForASInContext(context(), dl, addrspace))
 
 """
diff --git a/src/irbuilder.jl b/src/irbuilder.jl
index c236f22c..5f22ca42 100644
--- a/src/irbuilder.jl
+++ b/src/irbuilder.jl
@@ -523,15 +523,18 @@ not!(builder::IRBuilder, V::Value, Name::String="") =
 
 # re-implementation for flexibility (exposing addrspace, add_null)
 function globalstring!(mod::LLVM.Module, str::String, name::String="";
-                       addrspace::Union{Integer,Nothing}=nothing, add_null::Bool=true)
+        addrspace::Union{Integer, Nothing} = nothing, add_null::Bool = true
+    )
     bytes = Vector{UInt8}(str)
     if add_null
         push!(bytes, 0x00)
     end
     constant = ConstantDataArray(bytes)
 
-    gv = GlobalVariable(mod, value_type(constant), name,
-                        something(addrspace, globals_addrspace(datalayout(mod))))
+    gv = GlobalVariable(
+        mod, value_type(constant), name,
+        something(addrspace, globals_addrspace(datalayout(mod)))
+    )
     alignment!(gv, 1)
     unnamed_addr!(gv, true)
     initializer!(gv, constant)
diff --git a/test/datalayout_tests.jl b/test/datalayout_tests.jl
index ae1bab35..6c2ad4dd 100644
--- a/test/datalayout_tests.jl
+++ b/test/datalayout_tests.jl
@@ -3,44 +3,44 @@
 dlstr = "E-p:32:32-f128:128:128"
 
 let
-    dl = DataLayout(dlstr)
-    dispose(dl)
+        dl = DataLayout(dlstr)
+        dispose(dl)
 end
 
-DataLayout(dlstr) do dl
+    DataLayout(dlstr) do dl
 end
 
-@dispose ctx=Context() dl=DataLayout(dlstr) begin
-    @test string(dl) == dlstr
+    @dispose ctx = Context() dl = DataLayout(dlstr) begin
+        @test string(dl) == dlstr
 
-    @test occursin(dlstr, sprint(io->show(io,dl)))
+        @test occursin(dlstr, sprint(io -> show(io, dl)))
 
-    @test byteorder(dl) == LLVM.API.LLVMBigEndian
-    @test pointersize(dl) == pointersize(dl, 0) == 4
+        @test byteorder(dl) == LLVM.API.LLVMBigEndian
+        @test pointersize(dl) == pointersize(dl, 0) == 4
 
-    @test intptr(dl) == intptr(dl, 0) == LLVM.Int32Type()
+        @test intptr(dl) == intptr(dl, 0) == LLVM.Int32Type()
 
-    @test sizeof(dl, LLVM.Int32Type()) == storage_size(dl, LLVM.Int32Type()) == abi_size(dl, LLVM.Int32Type()) == 4
+        @test sizeof(dl, LLVM.Int32Type()) == storage_size(dl, LLVM.Int32Type()) == abi_size(dl, LLVM.Int32Type()) == 4
 
-    @test abi_alignment(dl, LLVM.Int32Type()) == frame_alignment(dl, LLVM.Int32Type()) == preferred_alignment(dl, LLVM.Int32Type()) == 4
+        @test abi_alignment(dl, LLVM.Int32Type()) == frame_alignment(dl, LLVM.Int32Type()) == preferred_alignment(dl, LLVM.Int32Type()) == 4
 
     @dispose mod=LLVM.Module("SomeModule") begin
         gv = GlobalVariable(mod, LLVM.Int32Type(), "SomeGlobal")
-        @test preferred_alignment(dl, gv) == 4
+            @test preferred_alignment(dl, gv) == 4
 
-        datalayout!(mod, dl)
-        @test string(datalayout(mod)) == string(dl)
+            datalayout!(mod, dl)
+            @test string(datalayout(mod)) == string(dl)
     end
 
     elem = [LLVM.Int32Type(), LLVM.FloatType()]
     let st = LLVM.StructType(elem)
-        @test element_at(dl, st, 4) == 1
-        @test offsetof(dl, st, 1) == 4
-    end
+            @test element_at(dl, st, 4) == 1
+            @test offsetof(dl, st, 1) == 4
+        end
 
-    @test globals_addrspace(dl) == 0
-    @dispose dl2=DataLayout(dlstr*"-G1") begin
-        @test globals_addrspace(dl2) == 1
+        @test globals_addrspace(dl) == 0
+        @dispose dl2 = DataLayout(dlstr * "-G1") begin
+            @test globals_addrspace(dl2) == 1
     end
 end
 

@codecov-commenter
Copy link

codecov-commenter commented Jun 16, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

@@           Coverage Diff           @@
##           master     #516   +/-   ##
=======================================
  Coverage   87.88%   87.89%           
=======================================
  Files          45       45           
  Lines        2700     2701    +1     
=======================================
+ Hits         2373     2374    +1     
  Misses        327      327           
Files with missing lines Coverage Δ
src/datalayout.jl 100.00% <100.00%> (ø)
src/irbuilder.jl 86.92% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@maleadt maleadt closed this Jun 17, 2025
@maleadt maleadt reopened this Jun 17, 2025
@maleadt maleadt force-pushed the tb/globalstring_addrspace branch from 0974438 to 764cc59 Compare June 17, 2025 05:08
@maleadt maleadt merged commit 8c02e45 into master Jun 17, 2025
45 of 50 checks passed
@maleadt maleadt deleted the tb/globalstring_addrspace branch June 17, 2025 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants