Use splat parameter to put Tuples in large Array constants#15495
Merged
straight-shoota merged 1 commit intocrystal-lang:masterfrom Feb 22, 2025
Merged
Conversation
straight-shoota
approved these changes
Feb 21, 2025
Member
straight-shoota
left a comment
There was a problem hiding this comment.
This is quite intriguing because one would expect that both variants essentially do the same thing and there's not much room for semantic difference. But apparently there is...
kojix2
pushed a commit
to kojix2/crystal
that referenced
this pull request
Feb 23, 2025
…al-lang#15495) We use a special `put` function instead of an array literal for populating large `Array` constants (see crystal-lang#4516), since this eliminates the individual `alloca`s for each array element. However, this is only true for simple literals, and tuple literals are not one of them. The following snippet: ```crystal def put(array : Array, values) : Nil array << values end N = 2 def foo data = Array({Int32, Int32, Int32}).new(N) {% for _ in 0...N %} put(data, {1, 2, 3}) {% end %} end foo ``` produces: ```llvm define internal void @"*foo:Nil"() #0 { alloca: %data = alloca ptr, align 8 %0 = alloca %"Tuple(Int32, Int32, Int32)", align 8 %1 = alloca %"Tuple(Int32, Int32, Int32)", align 8 br label %entry entry: ; preds = %alloca %2 = call ptr @"*Array(Tuple(Int32, Int32, Int32))@array(T)::new<Int32>:Array(Tuple(Int32, Int32, Int32))"(i32 844, i32 2) store ptr %2, ptr %data, align 8 %3 = load ptr, ptr %data, align 8 %4 = getelementptr inbounds %"Tuple(Int32, Int32, Int32)", ptr %0, i32 0, i32 0 store i32 1, ptr %4, align 4 %5 = getelementptr inbounds %"Tuple(Int32, Int32, Int32)", ptr %0, i32 0, i32 1 store i32 2, ptr %5, align 4 %6 = getelementptr inbounds %"Tuple(Int32, Int32, Int32)", ptr %0, i32 0, i32 2 store i32 3, ptr %6, align 4 %7 = load %"Tuple(Int32, Int32, Int32)", ptr %0, align 4 call void @"*put<Array(Tuple(Int32, Int32, Int32)), Tuple(Int32, Int32, Int32)>:Nil"(ptr %3, %"Tuple(Int32, Int32, Int32)" %7) %8 = load ptr, ptr %data, align 8 %9 = getelementptr inbounds %"Tuple(Int32, Int32, Int32)", ptr %1, i32 0, i32 0 store i32 1, ptr %9, align 4 %10 = getelementptr inbounds %"Tuple(Int32, Int32, Int32)", ptr %1, i32 0, i32 1 store i32 2, ptr %10, align 4 %11 = getelementptr inbounds %"Tuple(Int32, Int32, Int32)", ptr %1, i32 0, i32 2 store i32 3, ptr %11, align 4 %12 = load %"Tuple(Int32, Int32, Int32)", ptr %1, align 4 call void @"*put<Array(Tuple(Int32, Int32, Int32)), Tuple(Int32, Int32, Int32)>:Nil"(ptr %8, %"Tuple(Int32, Int32, Int32)" %12) ret void } ; Function Attrs: uwtable define internal void @"*put<Array(Tuple(Int32, Int32, Int32)), Tuple(Int32, Int32, Int32)>:Nil"(ptr %array, %"Tuple(Int32, Int32, Int32)" %values) #0 { alloca: %values1 = alloca %"Tuple(Int32, Int32, Int32)", align 8 br label %entry entry: ; preds = %alloca store %"Tuple(Int32, Int32, Int32)" %values, ptr %values1, align 4 %0 = load %"Tuple(Int32, Int32, Int32)", ptr %values1, align 4 %1 = call ptr @"*Array(Tuple(Int32, Int32, Int32))@array(T)#<<<Tuple(Int32, Int32, Int32)>:Array(Tuple(Int32, Int32, Int32))"(ptr %array, %"Tuple(Int32, Int32, Int32)" %0) ret void } ``` whereas this: ```crystal def put(array : Array, *values) : Nil array << values end N = 2 def foo data = Array({Int32, Int32, Int32}).new(N) {% for _ in 0...N %} put(data, 1, 2, 3) {% end %} end foo ``` produces: ```llvm ; Function Attrs: uwtable define internal void @"*foo:Nil"() #0 { alloca: %data = alloca ptr, align 8 br label %entry entry: ; preds = %alloca %0 = call ptr @"*Array(Tuple(Int32, Int32, Int32))@array(T)::new<Int32>:Array(Tuple(Int32, Int32, Int32))"(i32 844, i32 2) store ptr %0, ptr %data, align 8 %1 = load ptr, ptr %data, align 8 call void @"*put<Array(Tuple(Int32, Int32, Int32)), Int32, Int32, Int32>:Nil"(ptr %1, i32 1, i32 2, i32 3) %2 = load ptr, ptr %data, align 8 call void @"*put<Array(Tuple(Int32, Int32, Int32)), Int32, Int32, Int32>:Nil"(ptr %2, i32 1, i32 2, i32 3) ret void } ; Function Attrs: uwtable define internal void @"*put<Array(Tuple(Int32, Int32, Int32)), Int32, Int32, Int32>:Nil"(ptr %array, i32 %__temp_1436, i32 %__temp_1437, i32 %__temp_1438) #0 { alloca: %values = alloca %"Tuple(Int32, Int32, Int32)", align 8 %0 = alloca %"Tuple(Int32, Int32, Int32)", align 8 br label %entry entry: ; preds = %alloca %1 = getelementptr inbounds %"Tuple(Int32, Int32, Int32)", ptr %0, i32 0, i32 0 store i32 %__temp_1436, ptr %1, align 4 %2 = getelementptr inbounds %"Tuple(Int32, Int32, Int32)", ptr %0, i32 0, i32 1 store i32 %__temp_1437, ptr %2, align 4 %3 = getelementptr inbounds %"Tuple(Int32, Int32, Int32)", ptr %0, i32 0, i32 2 store i32 %__temp_1438, ptr %3, align 4 %4 = load %"Tuple(Int32, Int32, Int32)", ptr %0, align 4 store %"Tuple(Int32, Int32, Int32)" %4, ptr %values, align 4 %5 = load %"Tuple(Int32, Int32, Int32)", ptr %values, align 4 %6 = call ptr @"*Array(Tuple(Int32, Int32, Int32))@array(T)#<<<Tuple(Int32, Int32, Int32)>:Array(Tuple(Int32, Int32, Int32))"(ptr %array, %"Tuple(Int32, Int32, Int32)" %5) ret void } ``` The `alloca` and `getelementptr` instructions are moved into the `put` function. If we set `N = 10000` instead, this subtle change could reduce the bytecode generation phase's time for this snippet by as much as 0.7s on my Windows machine.
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We use a special
putfunction instead of an array literal for populating largeArrayconstants (see #4516), since this eliminates the individualallocas for each array element. However, this is only true for simple literals, and tuple literals are not one of them. The following snippet:produces:
LLVM IR
whereas this:
produces:
LLVM IR
The
allocaandgetelementptrinstructions are moved into theputfunction. If we setN = 10000instead, this subtle change could reduce the bytecode generation phase's time for this snippet by as much as 0.7s on my Windows machine.