-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[AMDGPU] Handled G_UBSANTRAP GlobalIsel #134492
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
base: main
Are you sure you want to change the base?
Conversation
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 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. |
@llvm/pr-subscribers-llvm-globalisel @llvm/pr-subscribers-backend-amdgpu Author: None (amansharma612) ChangesFixes #131740 Full diff: https://github.com/llvm/llvm-project/pull/134492.diff 3 Files Affected:
diff --git a/llvm/lib/Target/AMDGPU/AMDGPULegalizerInfo.cpp b/llvm/lib/Target/AMDGPU/AMDGPULegalizerInfo.cpp
index 275d0193452a5..dd8a010adb6df 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPULegalizerInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPULegalizerInfo.cpp
@@ -2103,7 +2103,7 @@ AMDGPULegalizerInfo::AMDGPULegalizerInfo(const GCNSubtarget &ST_,
getActionDefinitionsBuilder({G_MEMCPY, G_MEMCPY_INLINE, G_MEMMOVE, G_MEMSET})
.lower();
- getActionDefinitionsBuilder({G_TRAP, G_DEBUGTRAP}).custom();
+ getActionDefinitionsBuilder({G_TRAP, G_DEBUGTRAP, G_UBSANTRAP}).custom();
getActionDefinitionsBuilder({G_VASTART, G_VAARG, G_BRJT, G_JUMP_TABLE,
G_INDEXED_LOAD, G_INDEXED_SEXTLOAD,
@@ -2222,6 +2222,8 @@ bool AMDGPULegalizerInfo::legalizeCustom(
return legalizeTrap(MI, MRI, B);
case TargetOpcode::G_DEBUGTRAP:
return legalizeDebugTrap(MI, MRI, B);
+ case TargetOpcode::G_UBSANTRAP:
+ return legalizeUbsanTrap(MI, MRI, B);
default:
return false;
}
@@ -7045,6 +7047,28 @@ bool AMDGPULegalizerInfo::legalizeDebugTrap(MachineInstr &MI,
return true;
}
+bool AMDGPULegalizerInfo::legalizeUbsanTrap(MachineInstr &MI,
+ MachineRegisterInfo &MRI,
+ MachineIRBuilder &B) const {
+ // Is non-HSA path or trap-handler disabled? Then, report a warning
+ // accordingly
+ if (!ST.isTrapHandlerEnabled() ||
+ ST.getTrapHandlerAbi() != GCNSubtarget::TrapHandlerAbi::AMDHSA) {
+ DiagnosticInfoUnsupported NoTrap(B.getMF().getFunction(),
+ "ubsantrap handler not supported",
+ MI.getDebugLoc(), DS_Warning);
+ LLVMContext &Ctx = B.getMF().getFunction().getContext();
+ Ctx.diagnose(NoTrap);
+ } else {
+ // Insert trap instruction
+ B.buildInstr(AMDGPU::S_TRAP)
+ .addImm(static_cast<unsigned>(GCNSubtarget::TrapID::LLVMAMDHSATrap));
+ }
+
+ MI.eraseFromParent();
+ return true;
+}
+
bool AMDGPULegalizerInfo::legalizeBVHIntersectRayIntrinsic(
MachineInstr &MI, MachineIRBuilder &B) const {
MachineRegisterInfo &MRI = *B.getMRI();
diff --git a/llvm/lib/Target/AMDGPU/AMDGPULegalizerInfo.h b/llvm/lib/Target/AMDGPU/AMDGPULegalizerInfo.h
index 1f4e02b0d600a..076a66bb6012f 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPULegalizerInfo.h
+++ b/llvm/lib/Target/AMDGPU/AMDGPULegalizerInfo.h
@@ -244,6 +244,9 @@ class AMDGPULegalizerInfo final : public LegalizerInfo {
bool legalizeDebugTrap(MachineInstr &MI, MachineRegisterInfo &MRI,
MachineIRBuilder &B) const;
+ bool legalizeUbsanTrap(MachineInstr &MI, MachineRegisterInfo &MRI,
+ MachineIRBuilder &B) const;
+
bool legalizeIntrinsic(LegalizerHelper &Helper,
MachineInstr &MI) const override;
};
diff --git a/llvm/test/CodeGen/AMDGPU/ubsan_trap.ll b/llvm/test/CodeGen/AMDGPU/ubsan_trap.ll
new file mode 100644
index 0000000000000..9f961031a1ddb
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/ubsan_trap.ll
@@ -0,0 +1,7 @@
+; RUN: llc -mtriple=amdgcn-amd-amdhsa -global-isel < %s
+; LLVM ERROR: cannot select: G_UBSANTRAP 0 (in function: ubsan_trap)
+
+define void @ubsan_trap() {
+ call void @llvm.ubsantrap(i8 0)
+ ret void
+}
\ No newline at end of file
|
This is my first pull request here. Would appreciate any feedback on code and the tests. Thanks. |
.addImm(static_cast<unsigned>(GCNSubtarget::TrapID::LLVMAMDHSATrap)); | ||
} | ||
|
||
MI.eraseFromParent(); |
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.
Do you want it to be removed even trap handler is disabled?
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.
It has to be modified or erased otherwise it will be a legalization loop
; CHECK-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) | ||
; CHECK-NEXT: s_trap 2 | ||
; CHECK-NEXT: s_setpc_b64 s[30:31] | ||
call void @llvm.ubsantrap(i8 0) |
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.
add negative test
bool AMDGPULegalizerInfo::legalizeUbsanTrap(MachineInstr &MI, | ||
MachineRegisterInfo &MRI, | ||
MachineIRBuilder &B) const { |
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.
This code is identical to legalizeDebugTrap above, they should just use the same implementation function
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.
The trap ID differs in these functions. Is it still required to merge the definitions?
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.
Yes, you can just rename that function above to "legalizeDebugUbsanTrap" and select the TrapID based on the opcode. The warning can also just say "debugtrap/ubsantrap handler not supported".
@@ -0,0 +1,13 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5 | ||
; RUN: llc -mtriple=amdgcn-amd-amdhsa -global-isel < %s | FileCheck %s |
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.
This should probably just merge in with trap-abis.ll and/or trap.ll. You're not testing both dag (which looks untested) or the various ABI options
bool AMDGPULegalizerInfo::legalizeUbsanTrap(MachineInstr &MI, | ||
MachineRegisterInfo &MRI, | ||
MachineIRBuilder &B) const { |
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.
Yes, you can just rename that function above to "legalizeDebugUbsanTrap" and select the TrapID based on the opcode. The warning can also just say "debugtrap/ubsantrap handler not supported".
llvm/test/CodeGen/AMDGPU/trap.ll
Outdated
@@ -1,3 +1,4 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --function ubsantrap --version 5 |
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.
This test was not autogenerated, it needs to be manually updated
it looks like the script did not do anything anyway
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.
Should any specific manual checks be added to the ubsantrap function in this case?
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.
reverse ping
llvm/test/CodeGen/AMDGPU/trap.ll
Outdated
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line: | ||
; GCN: {{.*}} | ||
; GCN-WARNING: {{.*}} | ||
; HSA-TRAP: {{.*}} | ||
; MESA-TRAP: {{.*}} | ||
; NO-HSA-TRAP: {{.*}} | ||
; NO-MESA-TRAP: {{.*}} | ||
; NO-TRAP-BIT: {{.*}} | ||
; NOMESA-TRAP: {{.*}} | ||
; TRAP-BIT: {{.*}} |
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.
Something went wrong here, and there are no checks on the other function
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.
@arsenm I am unaware of how/what checks to add manually for this function, would appreciate inputs regarding it.
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.
This one is a special case you can't generate. You should be able to copy the checks from a debugtrap function
✅ With the latest revision this PR passed the C/C++ code formatter. |
llvm/test/CodeGen/AMDGPU/trap.ll
Outdated
; GCN-WARNING-1: warning: <unknown>:0:0: in function hsa_debugtrap void (ptr addrspace(1)): debugtrap handler not supported | ||
; GCN-WARNING-2: warning: <unknown>:0:0: in function hsa_debugtrap void (ptr addrspace(1)): debugtrap/ubsantrap handler not supported |
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.
Should fix the error messages to be consistent instead of checking two different warnings
// Is non-HSA path or trap-handler disabled? Then, report a warning | ||
// accordingly | ||
if (!ST.isTrapHandlerEnabled() || | ||
ST.getTrapHandlerAbi() != GCNSubtarget::TrapHandlerAbi::AMDHSA) { | ||
DiagnosticInfoUnsupported NoTrap(B.getMF().getFunction(), | ||
"debugtrap handler not supported", | ||
"debugtrap/ubsantrap handler not supported", |
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.
Don't deviate from the DAG's message
@@ -2103,7 +2103,7 @@ AMDGPULegalizerInfo::AMDGPULegalizerInfo(const GCNSubtarget &ST_, | ||
getActionDefinitionsBuilder({G_MEMCPY, G_MEMCPY_INLINE, G_MEMMOVE, G_MEMSET}) | ||
.lower(); | ||
|
||
getActionDefinitionsBuilder({G_TRAP, G_DEBUGTRAP}).custom(); | ||
getActionDefinitionsBuilder({G_TRAP, G_DEBUGTRAP, G_UBSANTRAP}).custom(); |
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.
We probably should add the default lower() action to LegalizerHelper for G_DEBUGTRAP and G_UBSANTRAP, which just replace the opcode with G_TRAP (this is what LegalizeDAG does as the default action)
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.
So should we replace the custom() call to lower() or add lower() as a fallback option?
llvm/test/CodeGen/AMDGPU/trap.ll
Outdated
;define amdgpu_kernel void @ubsantrap(ptr addrspace(1) nocapture readonly %arg0) { | ||
; store volatile i32 1, ptr addrspace(1) %arg0 | ||
; call void @llvm.ubsantrap(i8 0) | ||
; store volatile i32 2, ptr addrspace(1) %arg0 | ||
; ret void | ||
;} |
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.
Uncomment this
@arsenm requesting a review on this |
Fixes #131740