This is an archive of the discontinued LLVM Phabricator instance.

Legalise patchpoint arguments.
ClosedPublic

Authored by vext01 on Jul 7 2022, 3:15 AM.

Details

Reviewers
arsenm
Summary

This is similar to D125680, but for llvm.experimental.patchpoint (instead of llvm.experimental.stackmap).

Some test outputs changed, e.g. passing i16 65535 as a live caused:

-; CHECK-NEXT:   .long   -1
+; CHECK-NEXT:   .long   65535

I think this is OK. I think the runtime is expected to know the true size of the constant, and the higher order bits are undefined. Under this assumption, -1 and 65535 are equivalent for a i16.

[The runtime certainly can't rely on the size field in the stackmap record, which for this constant i16 is reporting as 8 bytes long. This is particularly confusing in this instance, as the small constant field can only hold 4-bytes! This bug was not introduced by this diff]

Diff Detail

Event Timeline

vext01 created this revision.Jul 7 2022, 3:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 7 2022, 3:15 AM
vext01 requested review of this revision.Jul 7 2022, 3:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 7 2022, 3:15 AM
vext01 edited the summary of this revision. (Show Details)Jul 7 2022, 3:15 AM
arsenm added inline comments.Jul 11 2022, 7:35 AM
llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp
5550–5551

dyn_cast to ConstantSDNode

5563–5565

No return after else

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
9378

You can just forward the type, no need to call getFrameIndexTy

llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
2204

dyn_cast to ConstantSDNode

vext01 added inline comments.Jul 11 2022, 8:57 AM
llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp
5550–5551

Forgive me. I'm still fairly new to C++, am I doing this right?

diff
diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp
index 4ee4ece7d8ef..89c0738f24b0 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp
@@ -5547,8 +5547,7 @@ SDValue DAGTypeLegalizer::ExpandIntOp_STACKMAP(SDNode *N, unsigned OpNo) {
   for (unsigned I = 0; I < OpNo; I++)
     NewOps.push_back(N->getOperand(I));
 
-  if (Op->getOpcode() == ISD::Constant) {
-    ConstantSDNode *CN = cast<ConstantSDNode>(Op);
+  if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
     EVT Ty = Op.getValueType();
     if (CN->getConstantIntValue()->getValue().getActiveBits() < 64) {
       NewOps.push_back(
@@ -5587,8 +5586,7 @@ SDValue DAGTypeLegalizer::ExpandIntOp_PATCHPOINT(SDNode *N, unsigned OpNo) {
   for (unsigned I = 0; I < OpNo; I++)
     NewOps.push_back(N->getOperand(I));
 
-  if (Op->getOpcode() == ISD::Constant) {
-    ConstantSDNode *CN = cast<ConstantSDNode>(Op);
+  if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
     EVT Ty = Op.getValueType();
     if (CN->getConstantIntValue()->getValue().getActiveBits() < 64) {
       NewOps.push_back(
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
index 25105ee2dbde..0dae6a06ef72 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
@@ -2201,12 +2201,11 @@ void SelectionDAGISel::pushStackMapLiveVariable(SmallVectorImpl<SDValue> &Ops,
   // nodes at DAG-construction time.
   assert(OpNode->getOpcode() != ISD::FrameIndex);
 
-  if (OpNode->getOpcode() == ISD::Constant) {
+  if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpNode)) {
     Ops.push_back(
         CurDAG->getTargetConstant(StackMaps::ConstantOp, DL, MVT::i64));
     Ops.push_back(
-        CurDAG->getTargetConstant(cast<ConstantSDNode>(OpNode)->getZExtValue(),
-                                  DL, OpVal.getValueType()));
+        CurDAG->getTargetConstant(CN->getZExtValue(), DL, OpVal.getValueType()));
   } else {
     Ops.push_back(OpVal);
   }

This makes tests fail, so can't be equivalent.

5563–5565

I'm not sure what you mean.

arsenm added inline comments.Jul 11 2022, 11:45 AM
llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp
5550–5551

The slight difference is ConstantSDNode accepts Constant and TargetConstant but I would expect consistency at this point

vext01 updated this revision to Diff 443943.Jul 12 2022, 7:53 AM

New diff fixing some of @arsenm's comments.

I'm still unsure what you mean by "No return after else".

llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp
5550–5551

The first two hunks of the above diff are fine, it's the last one that causes test failures.

Not sure why, but I've omitted that change for now anyway.

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
9378

Yep. Fixed.

arsenm added inline comments.Jul 13 2022, 8:33 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
9373–9374

This comment is kind of a lie. Pointer typed doesn't imply legality but I guess it doesn't really matter

vext01 updated this revision to Diff 444576.Jul 14 2022, 3:06 AM

Refactor to remove else after returns.

This should be ready now.

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
9373–9374

I see what you are saying, although the pointer itself should be legal (even if the pointee isn't).

I agree that it doesn't hugely matter.

arsenm accepted this revision.Jul 14 2022, 2:46 PM

LGTM with nits

llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp
5548

!CN

5550

Don't need braces

5587–5590

Ditto

This revision is now accepted and ready to land.Jul 14 2022, 2:46 PM
vext01 closed this revision.Jul 18 2022, 8:28 AM

This landed in 2e62a26