forked from rust-lang/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 39
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
[SOL] Implement syscall instruction #121
Merged
LucasSte
merged 3 commits into
anza-xyz:solana-rustc/18.1-2024-05-19
from
LucasSte:syscall-1
Dec 19, 2024
+143
−30
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,8 @@ enum NodeType : unsigned { | |
SELECT_CC, | ||
BR_CC, | ||
Wrapper, | ||
MEMCPY | ||
MEMCPY, | ||
SYSCALL, | ||
}; | ||
} | ||
|
||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
; RUN: llc -march=sbf < %s | FileCheck --check-prefix=CHECK-V0 %s | ||
; RUN: llc -march=sbf -mattr=+static-syscalls -show-mc-encoding < %s | FileCheck --check-prefix=CHECK-V3 %s | ||
|
||
|
||
; Function Attrs: nounwind | ||
define dso_local i32 @test(i32 noundef %a, i32 noundef %b) { | ||
entry: | ||
; CHECK-LABEL: test | ||
|
||
; CHECK-V0: call 2 | ||
; CHECK-V3 syscall 2 # encoding: [0x95,0x00,0x00,0x00,0x01,0x00,0x00,0x00] | ||
%syscall_1 = tail call i32 inttoptr (i64 2 to ptr)(i32 noundef %a, i32 noundef %b) | ||
|
||
; CHECK-V0: call 11 | ||
; CHECK-V3: syscall 11 # encoding: [0x95,0x00,0x00,0x00,0x0b,0x00,0x00,0x00] | ||
%syscall_2 = tail call i32 inttoptr (i64 11 to ptr)(i32 noundef %a, i32 noundef %b) | ||
|
||
; CHECK-V0: call 112 | ||
; CHECK-V3: syscall 112 | ||
%syscall_3 = tail call i32 inttoptr (i64 112 to ptr)(i32 noundef %a, i32 noundef %b) | ||
|
||
%add_1 = add i32 %syscall_1, %syscall_2 | ||
%add_2 = add i32 %add_1, %syscall_3 | ||
ret i32 %add_1 | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
; RUN: llc -march=sbf -mattr=+static-syscalls < %s | FileCheck --check-prefix=CHECK %s | ||
|
||
; Syscall declaration in C: | ||
; | ||
; int c_declaration(int a, int b) { | ||
; int (*const syscall)(int a, int b) = (void*)50; | ||
; return syscall(a, b); | ||
; } | ||
; The following is the unoptimized output from clang: | ||
|
||
define dso_local i32 @c_declaration(i32 noundef %a, i32 noundef %b) #0 { | ||
entry: | ||
%a.addr = alloca i32, align 4 | ||
%b.addr = alloca i32, align 4 | ||
%syscall = alloca ptr, align 8 | ||
store i32 %a, ptr %a.addr, align 4 | ||
store i32 %b, ptr %b.addr, align 4 | ||
store ptr inttoptr (i64 50 to ptr), ptr %syscall, align 8 | ||
%0 = load i32, ptr %a.addr, align 4 | ||
%1 = load i32, ptr %b.addr, align 4 | ||
|
||
; Ensure the syscall instruction is emitted | ||
; CHECK: syscall 50 | ||
|
||
%call = call i32 inttoptr (i64 50 to ptr)(i32 noundef %0, i32 noundef %1) | ||
ret i32 %call | ||
} | ||
|
||
; Syscall declaration in Rust: | ||
; | ||
; #[no_mangle] | ||
; pub unsafe fn rust_declaration(b: u64) -> u32 { | ||
; let syscall : extern "C" fn(b: u64) -> u32 = core::mem::transmute(60u64); | ||
; return syscall(b); | ||
; } | ||
; The following is the unoptimized output from rustc: | ||
|
||
define i32 @rust_declaration(i64 %b) unnamed_addr { | ||
start: | ||
%syscall.dbg.spill = alloca [8 x i8], align 8 | ||
%b.dbg.spill = alloca [8 x i8], align 8 | ||
store i64 %b, ptr %b.dbg.spill, align 8 | ||
store ptr getelementptr (i8, ptr null, i64 60), ptr %syscall.dbg.spill, align 8 | ||
|
||
; Ensure the syscall instruction is emitted | ||
; CHECK: syscall 60 | ||
|
||
%_0 = call i32 getelementptr (i8, ptr null, i64 60)(i64 %b) | ||
ret i32 %_0 | ||
} |
This file contains 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
This file contains 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
This file contains 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
2 changes: 2 additions & 0 deletions
2
llvm/test/MC/SBF/sbf-return.s → llvm/test/MC/SBF/sbf-return-syscall.s
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
# RUN: llvm-mc -triple=sbf-solana-solana --mcpu=sbfv2 -filetype=obj -o %t %s | ||
# RUN: llvm-objdump -d -r %t | FileCheck --check-prefix=CHECK %s | ||
|
||
syscall 9 | ||
return | ||
|
||
// CHECK: 95 00 00 00 09 00 00 00 syscall 0x9 | ||
// CHECK: 9d 00 00 00 00 00 00 00 return |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In some circumstances, specially when we build the code with
-O0
, the front end may emit acallx
. Instead of creating a peephole or forcing optimizations in the backend, I decided to add the correct declaration example in both C and Rust.These examples generate LLVM-IR code that even when unoptimized will create a
syscall
instruction.