-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[AArch64] Implement "rZ" inline asm constraint #166022
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
Open
miloserdow
wants to merge
1
commit into
llvm:main
Choose a base branch
from
miloserdow:inline-asm-zero-reg
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+152
−0
Open
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 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
19 changes: 19 additions & 0 deletions
19
clang/test/CodeGen/aarch64-inline-asm-rZ-constraint-error.c
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // RUN: not %clang_cc1 -triple aarch64-linux-gnu -O2 -S -o /dev/null %s 2>&1 | FileCheck %s | ||
|
|
||
| // Test that the "rZ" inline assembly constraint properly rejects non-constant values. | ||
| // The "rZ" constraint is only valid for literal zero values. | ||
|
|
||
| // CHECK: error: invalid operand for inline asm constraint 'rZ' | ||
| void test_rZ_runtime_value(long *addr, long val) { | ||
| __asm__ volatile("str %1, [%0]" : : "r"(addr), "rZ"(val)); | ||
| } | ||
|
|
||
| // CHECK: error: invalid operand for inline asm constraint 'rZ' | ||
| void test_rZ_runtime_i32(int *addr, int val) { | ||
| __asm__ volatile("str %w1, [%0]" : : "r"(addr), "rZ"(val)); | ||
| } | ||
|
|
||
| // CHECK: error: invalid operand for inline asm constraint 'rZ' | ||
| void test_rZ_non_constant(long val) { | ||
| __asm__ volatile("mov x2, %0" : : "rZ"(val)); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // RUN: %clang_cc1 -triple aarch64-linux-gnu -O2 -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK-IR | ||
| // RUN: %clang_cc1 -triple aarch64-linux-gnu -O2 -S -o - %s | FileCheck %s --check-prefix=CHECK-ASM | ||
|
|
||
| // Test the "rZ" inline assembly constraint for AArch64. | ||
|
|
||
| // CHECK-IR-LABEL: define dso_local void @test_rZ_zero_i64( | ||
| // CHECK-IR: tail call void asm sideeffect "str $1, [$0]", "r,^rZ"(ptr %addr, i64 0) | ||
| // | ||
| // CHECK-ASM-LABEL: test_rZ_zero_i64: | ||
| // CHECK-ASM: str xzr, [x0] | ||
| void test_rZ_zero_i64(long *addr) { | ||
| __asm__ volatile("str %1, [%0]" : : "r"(addr), "rZ"(0L)); | ||
| } | ||
|
|
||
| // CHECK-IR-LABEL: define dso_local void @test_rZ_zero_i32( | ||
| // CHECK-IR: tail call void asm sideeffect "str ${1:w}, [$0]", "r,^rZ"(ptr %addr, i32 0) | ||
| // | ||
| // CHECK-ASM-LABEL: test_rZ_zero_i32: | ||
| // CHECK-ASM: str wzr, [x0] | ||
| void test_rZ_zero_i32(int *addr) { | ||
| __asm__ volatile("str %w1, [%0]" : : "r"(addr), "rZ"(0)); | ||
| } | ||
|
|
||
| // CHECK-IR-LABEL: define dso_local void @test_rZ_zero_i16( | ||
| // CHECK-IR: tail call void asm sideeffect "strh ${1:w}, [$0]", "r,^rZ"(ptr %addr, i16 0) | ||
| // | ||
| // CHECK-ASM-LABEL: test_rZ_zero_i16: | ||
| // CHECK-ASM: strh wzr, [x0] | ||
| void test_rZ_zero_i16(short *addr) { | ||
| __asm__ volatile("strh %w1, [%0]" : : "r"(addr), "rZ"((short)0)); | ||
| } | ||
|
|
||
| // CHECK-IR-LABEL: define dso_local void @test_rZ_zero_i8( | ||
| // CHECK-IR: tail call void asm sideeffect "strb ${1:w}, [$0]", "r,^rZ"(ptr %addr, i8 0) | ||
| // | ||
| // CHECK-ASM-LABEL: test_rZ_zero_i8: | ||
| // CHECK-ASM: strb wzr, [x0] | ||
| void test_rZ_zero_i8(char *addr) { | ||
| __asm__ volatile("strb %w1, [%0]" : : "r"(addr), "rZ"((char)0)); | ||
| } | ||
|
|
||
| // CHECK-IR-LABEL: define dso_local void @test_rz_lowercase( | ||
| // CHECK-IR: tail call void asm sideeffect "str $1, [$0]", "r,^rz"(ptr %addr, i64 0) | ||
| // | ||
| // CHECK-ASM-LABEL: test_rz_lowercase: | ||
| // CHECK-ASM: str xzr, [x0] | ||
| void test_rz_lowercase(long *addr) { | ||
| __asm__ volatile("str %1, [%0]" : : "r"(addr), "rz"(0L)); | ||
| } | ||
|
|
||
| // CHECK-IR-LABEL: define dso_local void @test_rZ_explicit_x( | ||
| // CHECK-IR: tail call void asm sideeffect "mov ${0:x}, xzr", "^rZ"(i64 0) | ||
| // | ||
| // CHECK-ASM-LABEL: test_rZ_explicit_x: | ||
| // CHECK-ASM: mov xzr, xzr | ||
| void test_rZ_explicit_x(void) { | ||
| __asm__ volatile("mov %x0, xzr" : : "rZ"(0L)); | ||
| } | ||
|
|
||
| // CHECK-IR-LABEL: define dso_local void @test_rZ_explicit_w( | ||
| // CHECK-IR: tail call void asm sideeffect "mov ${0:w}, wzr", "^rZ"(i32 0) | ||
| // | ||
| // CHECK-ASM-LABEL: test_rZ_explicit_w: | ||
| // CHECK-ASM: mov wzr, wzr | ||
| void test_rZ_explicit_w(void) { | ||
| __asm__ volatile("mov %w0, wzr" : : "rZ"(0)); | ||
| } | ||
|
|
||
| // CHECK-IR-LABEL: define dso_local void @test_rZ_x_modifier( | ||
| // CHECK-IR: tail call void asm sideeffect "add x2, x1, ${0:x}", "^rZ"(i64 0) | ||
| // | ||
| // CHECK-ASM-LABEL: test_rZ_x_modifier: | ||
| // CHECK-ASM: add x2, x1, xzr | ||
| void test_rZ_x_modifier(void) { | ||
| __asm__ volatile("add x2, x1, %x0" : : "rZ"(0L)); | ||
| } | ||
|
|
||
| // CHECK-IR-LABEL: define dso_local void @test_rZ_w_modifier( | ||
| // CHECK-IR: tail call void asm sideeffect "add w2, w1, ${0:w}", "^rZ"(i32 0) | ||
| // | ||
| // CHECK-ASM-LABEL: test_rZ_w_modifier: | ||
| // CHECK-ASM: add w2, w1, wzr | ||
| void test_rZ_w_modifier(void) { | ||
| __asm__ volatile("add w2, w1, %w0" : : "rZ"(0)); | ||
| } | ||
|
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13018,6 +13018,9 @@ AArch64TargetLowering::getConstraintType(StringRef Constraint) const { | |
| case 'S': // A symbol or label reference with a constant offset | ||
| return C_Other; | ||
| } | ||
| } else if (Constraint.size() == 2 && | ||
| (Constraint == "rZ" || Constraint == "rz")) { | ||
| return C_Other; | ||
| } else if (parsePredicateConstraint(Constraint)) | ||
| return C_RegisterClass; | ||
| else if (parseReducedGprConstraint(Constraint)) | ||
|
|
@@ -13045,6 +13048,14 @@ AArch64TargetLowering::getSingleConstraintMatchWeight( | |
| default: | ||
| weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); | ||
| break; | ||
| case 'r': | ||
| // Check for "rZ" or "rz" constraint (register or zero) | ||
| if (constraint[1] == 'Z' || constraint[1] == 'z') { | ||
| weight = CW_Register; | ||
| break; | ||
| } | ||
| weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); | ||
| break; | ||
| case 'x': | ||
| case 'w': | ||
| case 'y': | ||
|
|
@@ -13196,6 +13207,23 @@ void AArch64TargetLowering::LowerAsmOperandForConstraint( | |
| SelectionDAG &DAG) const { | ||
| SDValue Result; | ||
|
|
||
| // Handle "rZ" and "rz" constraints (register or zero) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of explicitly handling null here, can we just use the correct register class? Not that it's likely to make much of a difference, but the intent would be more clear. |
||
| if (Constraint.size() == 2 && Constraint[0] == 'r' && | ||
| (Constraint[1] == 'Z' || Constraint[1] == 'z')) { | ||
| if (isNullConstant(Op)) { | ||
| if (Op.getValueType() == MVT::i64) | ||
| Result = DAG.getRegister(AArch64::XZR, MVT::i64); | ||
| else | ||
| Result = DAG.getRegister(AArch64::WZR, MVT::i32); | ||
|
|
||
| if (Result.getNode()) { | ||
| Ops.push_back(Result); | ||
| return; | ||
| } | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| // Currently only support length 1 constraints. | ||
| if (Constraint.size() != 1) | ||
| 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.
Why C_Other?