Skip to content

[CodeGen] @llvm.experimental.stackmap make operands immediate #117932

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 1 commit into from
Dec 11, 2024

Conversation

Atafid
Copy link
Contributor

@Atafid Atafid commented Nov 27, 2024

This pull request modifies the behavior of the @llvm.experimental.stackmap intrinsic to require that its two first operands (id and numShadowBytes) be immediate values. This change ensures that variables cannot be passed as two first arguments to this intrinsic.

Related Issue: #115733

Testing

  • Added new test cases to ensure errors are emitted for non-immediate operands.
  • Ran the full LLVM test suite to verify no regressions were introduced.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Nov 27, 2024

@llvm/pr-subscribers-backend-powerpc
@llvm/pr-subscribers-backend-x86
@llvm/pr-subscribers-backend-aarch64

@llvm/pr-subscribers-backend-systemz

Author: Guillaume DI FATTA (Atafid)

Changes

This pull request modifies the behavior of the @<!-- -->llvm.experimental.stackmap intrinsic to require that its two first operands (id and numShadowBytes) be immediate values. This change ensures that variables cannot be passed as two first arguments to this intrinsic.

Related Issue: #115733

Testing

  • Added new test cases to ensure errors are emitted for non-immediate operands.
  • Ran the full LLVM test suite to verify no regressions were introduced.

Full diff: https://github.com/llvm/llvm-project/pull/117932.diff

6 Files Affected:

  • (modified) llvm/include/llvm/IR/Intrinsics.td (+1-1)
  • (added) llvm/test/CodeGen/AArch64/stackmap-args.ll (+44)
  • (added) llvm/test/CodeGen/PowerPC/ppc64-stackmap-args.ll (+58)
  • (added) llvm/test/CodeGen/RISCV/rv64-stackmap-args.ll (+32)
  • (added) llvm/test/CodeGen/SystemZ/stackmap-args.ll (+44)
  • (added) llvm/test/CodeGen/X86/stackmap-args.ll (+44)
diff --git a/llvm/include/llvm/IR/Intrinsics.td b/llvm/include/llvm/IR/Intrinsics.td
index 1ca8c2565ab0b6..ee877349a33149 100644
--- a/llvm/include/llvm/IR/Intrinsics.td
+++ b/llvm/include/llvm/IR/Intrinsics.td
@@ -1655,7 +1655,7 @@ def int_strip_invariant_group : DefaultAttrsIntrinsic<[llvm_anyptr_ty],
 //
 def int_experimental_stackmap : DefaultAttrsIntrinsic<[],
                                   [llvm_i64_ty, llvm_i32_ty, llvm_vararg_ty],
-                                  [Throws]>;
+                                  [Throws, ImmArg<ArgIndex<0>>, ImmArg<ArgIndex<1>>]>;
 def int_experimental_patchpoint_void : Intrinsic<[],
                                                  [llvm_i64_ty, llvm_i32_ty,
                                                   llvm_ptr_ty, llvm_i32_ty,
diff --git a/llvm/test/CodeGen/AArch64/stackmap-args.ll b/llvm/test/CodeGen/AArch64/stackmap-args.ll
new file mode 100644
index 00000000000000..0a7b3a4ab1a329
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/stackmap-args.ll
@@ -0,0 +1,44 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc < %s -mtriple=arm64-linux-gnu | FileCheck %s
+; This test is expected to fail.
+; XFAIL:*
+
+; Tests failure when we pass non-immediate args to @llvm.experiment.stackmap
+
+define void @first_arg() {
+; CHECK-LABEL: first_arg:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    stp x29, x30, [sp, #-16]! // 16-byte Folded Spill
+; CHECK-NEXT:    mov x29, sp
+; CHECK-NEXT:    .cfi_def_cfa w29, 16
+; CHECK-NEXT:    .cfi_offset w30, -8
+; CHECK-NEXT:    .cfi_offset w29, -16
+; CHECK-NEXT:  .Ltmp0:
+; CHECK-NEXT:    ldp x29, x30, [sp], #16 // 16-byte Folded Reload
+; CHECK-NEXT:    ret
+entry:
+  ; First operand should be immediate
+  %id = add i64 0, 0
+  call void (i64, i32, ...) @llvm.experimental.stackmap(i64 %id, i32 0)
+  ret void
+}
+
+define void @second_arg() {
+; CHECK-LABEL: second_arg:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    stp x29, x30, [sp, #-16]! // 16-byte Folded Spill
+; CHECK-NEXT:    mov x29, sp
+; CHECK-NEXT:    .cfi_def_cfa w29, 16
+; CHECK-NEXT:    .cfi_offset w30, -8
+; CHECK-NEXT:    .cfi_offset w29, -16
+; CHECK-NEXT:  .Ltmp1:
+; CHECK-NEXT:    ldp x29, x30, [sp], #16 // 16-byte Folded Reload
+; CHECK-NEXT:    ret
+entry:
+  ; Second operand should be immediate
+  %numShadowByte = add i32 0, 0
+  call void (i64, i32, ...) @llvm.experimental.stackmap(i64 1, i32 %numShadowByte)
+  ret void
+}
+
+declare void @llvm.experimental.stackmap(i64, i32, ...)
diff --git a/llvm/test/CodeGen/PowerPC/ppc64-stackmap-args.ll b/llvm/test/CodeGen/PowerPC/ppc64-stackmap-args.ll
new file mode 100644
index 00000000000000..597cc7edf16e18
--- /dev/null
+++ b/llvm/test/CodeGen/PowerPC/ppc64-stackmap-args.ll
@@ -0,0 +1,58 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc -verify-machineinstrs < %s -mcpu=ppc -mtriple=powerpc64-unknown-gnu-linux | FileCheck %s
+; This test is expected to fail.
+; XFAIL:*
+
+; Tests failure when we pass non-immediate args to @llvm.experiment.stackmap
+
+define void @first_arg() {
+; CHECK-LABEL: first_arg:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    mflr 0
+; CHECK-NEXT:    std 31, -8(1)
+; CHECK-NEXT:    stdu 1, -64(1)
+; CHECK-NEXT:    std 0, 80(1)
+; CHECK-NEXT:    .cfi_def_cfa_offset 64
+; CHECK-NEXT:    .cfi_offset r31, -8
+; CHECK-NEXT:    .cfi_offset lr, 16
+; CHECK-NEXT:    mr 31, 1
+; CHECK-NEXT:    .cfi_def_cfa_register r31
+; CHECK-NEXT:  .Ltmp0:
+; CHECK-NEXT:    addi 1, 1, 64
+; CHECK-NEXT:    ld 0, 16(1)
+; CHECK-NEXT:    ld 31, -8(1)
+; CHECK-NEXT:    mtlr 0
+; CHECK-NEXT:    blr
+entry:
+  ; First operand should be immediate
+  %id = add i64 0, 0
+  call void (i64, i32, ...) @llvm.experimental.stackmap(i64 %id, i32 0)
+  ret void
+}
+
+define void @second_arg() {
+; CHECK-LABEL: second_arg:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    mflr 0
+; CHECK-NEXT:    std 31, -8(1)
+; CHECK-NEXT:    stdu 1, -64(1)
+; CHECK-NEXT:    std 0, 80(1)
+; CHECK-NEXT:    .cfi_def_cfa_offset 64
+; CHECK-NEXT:    .cfi_offset r31, -8
+; CHECK-NEXT:    .cfi_offset lr, 16
+; CHECK-NEXT:    mr 31, 1
+; CHECK-NEXT:    .cfi_def_cfa_register r31
+; CHECK-NEXT:  .Ltmp1:
+; CHECK-NEXT:    addi 1, 1, 64
+; CHECK-NEXT:    ld 0, 16(1)
+; CHECK-NEXT:    ld 31, -8(1)
+; CHECK-NEXT:    mtlr 0
+; CHECK-NEXT:    blr
+entry:
+  ; Second operand should be immediate
+  %numShadowByte = add i32 0, 0
+  call void (i64, i32, ...) @llvm.experimental.stackmap(i64 1, i32 %numShadowByte)
+  ret void
+}
+
+declare void @llvm.experimental.stackmap(i64, i32, ...)
diff --git a/llvm/test/CodeGen/RISCV/rv64-stackmap-args.ll b/llvm/test/CodeGen/RISCV/rv64-stackmap-args.ll
new file mode 100644
index 00000000000000..1fdf56a563f121
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/rv64-stackmap-args.ll
@@ -0,0 +1,32 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc -mtriple=riscv64 < %s | FileCheck %s
+; This test is expected to fail.
+; XFAIL:*
+
+; Tests failure when we pass non-immediate args to @llvm.experiment.stackmap
+
+define void @first_arg() {
+; CHECK-LABEL: first_arg:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:  .Ltmp0:
+; CHECK-NEXT:    ret
+entry:
+  ; First operand should be immediate
+  %id = add i64 0, 0
+  call void (i64, i32, ...) @llvm.experimental.stackmap(i64 %id, i32 0)
+  ret void
+}
+
+define void @second_arg() {
+; CHECK-LABEL: second_arg:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:  .Ltmp1:
+; CHECK-NEXT:    ret
+entry:
+  ; Second operand should be immediate
+  %numShadowByte = add i32 0, 0
+  call void (i64, i32, ...) @llvm.experimental.stackmap(i64 1, i32 %numShadowByte)
+  ret void
+}
+
+declare void @llvm.experimental.stackmap(i64, i32, ...)
diff --git a/llvm/test/CodeGen/SystemZ/stackmap-args.ll b/llvm/test/CodeGen/SystemZ/stackmap-args.ll
new file mode 100644
index 00000000000000..1b7767e15eab86
--- /dev/null
+++ b/llvm/test/CodeGen/SystemZ/stackmap-args.ll
@@ -0,0 +1,44 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc < %s -mtriple=s390x-linux-gnu | FileCheck %s
+; This test is expected to fail.
+; XFAIL:*
+
+; Tests failure when we pass non-immediate args to @llvm.experiment.stackmap
+
+define void @first_arg() {
+; CHECK-LABEL: first_arg:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    stmg %r14, %r15, 112(%r15)
+; CHECK-NEXT:    .cfi_offset %r14, -48
+; CHECK-NEXT:    .cfi_offset %r15, -40
+; CHECK-NEXT:    aghi %r15, -160
+; CHECK-NEXT:    .cfi_def_cfa_offset 320
+; CHECK-NEXT:  .Ltmp0:
+; CHECK-NEXT:    lmg %r14, %r15, 272(%r15)
+; CHECK-NEXT:    br %r14
+entry:
+  ; First operand should be immediate
+  %id = add i64 0, 0
+  call void (i64, i32, ...) @llvm.experimental.stackmap(i64 %id, i32 0)
+  ret void
+}
+
+define void @second_arg() {
+; CHECK-LABEL: second_arg:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    stmg %r14, %r15, 112(%r15)
+; CHECK-NEXT:    .cfi_offset %r14, -48
+; CHECK-NEXT:    .cfi_offset %r15, -40
+; CHECK-NEXT:    aghi %r15, -160
+; CHECK-NEXT:    .cfi_def_cfa_offset 320
+; CHECK-NEXT:  .Ltmp1:
+; CHECK-NEXT:    lmg %r14, %r15, 272(%r15)
+; CHECK-NEXT:    br %r14
+entry:
+  ; Second operand should be immediate
+  %numShadowByte = add i32 0, 0
+  call void (i64, i32, ...) @llvm.experimental.stackmap(i64 1, i32 %numShadowByte)
+  ret void
+}
+
+declare void @llvm.experimental.stackmap(i64, i32, ...)
diff --git a/llvm/test/CodeGen/X86/stackmap-args.ll b/llvm/test/CodeGen/X86/stackmap-args.ll
new file mode 100644
index 00000000000000..99aefeb8701f2a
--- /dev/null
+++ b/llvm/test/CodeGen/X86/stackmap-args.ll
@@ -0,0 +1,44 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc < %s -mtriple=x86_64-apple-darwin -mcpu=corei7 | FileCheck %s
+; This test is expected to fail.
+; XFAIL:*
+
+; Tests failure when we pass non-immediate args to @llvm.experiment.stackmap
+
+define void @first_arg() {
+; CHECK-LABEL: first_arg:
+; CHECK:       ## %bb.0: ## %entry
+; CHECK-NEXT:    pushq %rbp
+; CHECK-NEXT:    .cfi_def_cfa_offset 16
+; CHECK-NEXT:    .cfi_offset %rbp, -16
+; CHECK-NEXT:    movq %rsp, %rbp
+; CHECK-NEXT:    .cfi_def_cfa_register %rbp
+; CHECK-NEXT:  Ltmp0:
+; CHECK-NEXT:    popq %rbp
+; CHECK-NEXT:    retq
+entry:
+  ; First operand should be immediate
+  %id = add i64 0, 0
+  call void (i64, i32, ...) @llvm.experimental.stackmap(i64 %id, i32 0)
+  ret void
+}
+
+define void @second_arg() {
+; CHECK-LABEL: second_arg:
+; CHECK:       ## %bb.0: ## %entry
+; CHECK-NEXT:    pushq %rbp
+; CHECK-NEXT:    .cfi_def_cfa_offset 16
+; CHECK-NEXT:    .cfi_offset %rbp, -16
+; CHECK-NEXT:    movq %rsp, %rbp
+; CHECK-NEXT:    .cfi_def_cfa_register %rbp
+; CHECK-NEXT:  Ltmp1:
+; CHECK-NEXT:    popq %rbp
+; CHECK-NEXT:    retq
+entry:
+  ; Second operand should be immediate
+  %numShadowByte = add i32 0, 0
+  call void (i64, i32, ...) @llvm.experimental.stackmap(i64 1, i32 %numShadowByte)
+  ret void
+}
+
+declare void @llvm.experimental.stackmap(i64, i32, ...)

@uweigand
Copy link
Member

I don't have any comments on the actual change, but this part:

Added new test cases to ensure errors are emitted for non-immediate operands.

doesn't really match the PR - you add several tests, but they are all XFAILed, so they don't check for specific errors.

Usually, to check for a specific error message, you can use the "RUN: not" ... pattern, see e.g. test/CodeGen/SystemZ/soft-float-inline-asm-01.ll:

; RUN: not llc < %s -mtriple=s390x-linux-gnu -mcpu=z13 -mattr=soft-float -O3 2>&1 | FileCheck %s
; 
; Verify that inline asms cannot use fp/vector registers with soft-float.

define float @f1() {
  %ret = call float asm "", "=f" ()
  ret float %ret
}

; CHECK: error: couldn't allocate output register for constraint 'f'

@Atafid
Copy link
Contributor Author

Atafid commented Nov 28, 2024

Thank you I didn't know this pattern, I used it to update my tests.
I squashed the commits into one to keep the history clean. Let me know if further changes are needed

@Atafid
Copy link
Contributor Author

Atafid commented Dec 5, 2024

@uweigand @phoebewang
Hello, sorry for the cc, have you any other comments on what I wrote ? :)

@uweigand
Copy link
Member

uweigand commented Dec 5, 2024

The test cases look good to me now. I don't have any additional comments.

Copy link
Contributor

@phoebewang phoebewang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Patch LG.

I didn't find a description in LangRef. Would be great if it's completed.

@Atafid
Copy link
Contributor Author

Atafid commented Dec 10, 2024

The documentation seems to be in llvm/docs/StackMaps.rst, I added a sentence to inform that the operands should be immediate.
Again I squashed the commits into one to keep the history clean.

@phoebewang
Copy link
Contributor

The documentation seems to be in llvm/docs/StackMaps.rst, I added a sentence to inform that the operands should be immediate. Again I squashed the commits into one to keep the history clean.

Thanks for the update! Do you need me to merge the patch for you?

@Atafid
Copy link
Contributor Author

Atafid commented Dec 11, 2024

If you have no further comments, I would really appreciate it if you could merge it. Thank you! :)

@phoebewang phoebewang merged commit a1ee1a9 into llvm:main Dec 11, 2024
9 checks passed
Copy link

@Atafid Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@phoebewang
Copy link
Contributor

If you have no further comments, I would really appreciate it if you could merge it. Thank you! :)

You are welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants