[llvm] 3b6dfa3 - [RISCV] Protect the SHL/SRA/SRL handlers in LowerOperation against being called for an illegal i32 shift amount.
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 29 09:55:12 PDT 2021
Author: Craig Topper
Date: 2021-06-29T09:45:13-07:00
New Revision: 3b6dfa381edfc66864cfd6dbc2769ba645858120
URL: https://github.com/llvm/llvm-project/commit/3b6dfa381edfc66864cfd6dbc2769ba645858120
DIFF: https://github.com/llvm/llvm-project/commit/3b6dfa381edfc66864cfd6dbc2769ba645858120.diff
LOG: [RISCV] Protect the SHL/SRA/SRL handlers in LowerOperation against being called for an illegal i32 shift amount.
It seems it is possible for DAG combine to create a shl with an
i64 result type and an i32 shift amount. This is ok before type
legalization since the type don't need to match in SelectionDAG.
This results in type legalization calling LowerOperation to
legalize just the amount. We weren't expecting this so we
asserted for not finding a fixed vector shift.
To fix this, I've added a check for the fixed vector case and
returned SDValue() to get the default type legalizer. I've
factored all shifts together and added a fixed vector specific
handler to avoid repeating similar code for each in
LowerOperation.
The particular case I found was exposed by D104581, but the bad
shift is created after that patch triggers.
Added:
Modified:
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
llvm/lib/Target/RISCV/RISCVISelLowering.h
llvm/test/CodeGen/RISCV/aext-to-sext.ll
Removed:
################################################################################
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index 3cf3ad958f691..30c2224bf8b23 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -2432,11 +2432,14 @@ SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
case ISD::UREM:
return lowerToScalableOp(Op, DAG, RISCVISD::UREM_VL);
case ISD::SHL:
- return lowerToScalableOp(Op, DAG, RISCVISD::SHL_VL);
case ISD::SRA:
- return lowerToScalableOp(Op, DAG, RISCVISD::SRA_VL);
case ISD::SRL:
- return lowerToScalableOp(Op, DAG, RISCVISD::SRL_VL);
+ if (Op.getSimpleValueType().isFixedLengthVector())
+ return lowerFixedLengthVectorShiftToRVV(Op, DAG);
+ // This can be called for an i32 shift amount that needs to be promoted.
+ assert(Op.getOperand(1).getValueType() == MVT::i32 && Subtarget.is64Bit() &&
+ "Unexpected custom legalisation");
+ return SDValue();
case ISD::FADD:
return lowerToScalableOp(Op, DAG, RISCVISD::FADD_VL);
case ISD::FSUB:
@@ -4290,6 +4293,20 @@ SDValue RISCVTargetLowering::lowerFixedLengthVectorLogicOpToRVV(
return lowerToScalableOp(Op, DAG, VecOpc, /*HasMask*/ true);
}
+SDValue
+RISCVTargetLowering::lowerFixedLengthVectorShiftToRVV(SDValue Op,
+ SelectionDAG &DAG) const {
+ unsigned Opc;
+ switch (Op.getOpcode()) {
+ default: llvm_unreachable("Unexpected opcode!");
+ case ISD::SHL: Opc = RISCVISD::SHL_VL; break;
+ case ISD::SRA: Opc = RISCVISD::SRA_VL; break;
+ case ISD::SRL: Opc = RISCVISD::SRL_VL; break;
+ }
+
+ return lowerToScalableOp(Op, DAG, Opc);
+}
+
// Lower vector ABS to smax(X, sub(0, X)).
SDValue RISCVTargetLowering::lowerABS(SDValue Op, SelectionDAG &DAG) const {
SDLoc DL(Op);
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.h b/llvm/lib/Target/RISCV/RISCVISelLowering.h
index 8fc92f65c38f3..cd13e748fed37 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.h
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.h
@@ -552,6 +552,7 @@ class RISCVTargetLowering : public TargetLowering {
SDValue lowerFixedLengthVectorLogicOpToRVV(SDValue Op, SelectionDAG &DAG,
unsigned MaskOpc,
unsigned VecOpc) const;
+ SDValue lowerFixedLengthVectorShiftToRVV(SDValue Op, SelectionDAG &DAG) const;
SDValue lowerFixedLengthVectorSelectToRVV(SDValue Op,
SelectionDAG &DAG) const;
SDValue lowerToScalableOp(SDValue Op, SelectionDAG &DAG, unsigned NewOpc,
diff --git a/llvm/test/CodeGen/RISCV/aext-to-sext.ll b/llvm/test/CodeGen/RISCV/aext-to-sext.ll
index 45b30dd60a3d5..5265a085073a7 100644
--- a/llvm/test/CodeGen/RISCV/aext-to-sext.ll
+++ b/llvm/test/CodeGen/RISCV/aext-to-sext.ll
@@ -46,3 +46,32 @@ bb6: ; preds = %bb2, %bb
}
declare void @hoge()
+
+; This ends up creating a shl with a i64 result type, but an i32 shift amount.
+; Because custom type legalization for i32 is enabled, this resulted in
+; LowerOperation being called for the amount. This was not expected and
+; triggered an assert.
+define i32 @crash(i32 %x, i32 %y, i32 %z) {
+; RV64I-LABEL: crash:
+; RV64I: # %bb.0:
+; RV64I-NEXT: sext.w a0, a0
+; RV64I-NEXT: seqz a3, a0
+; RV64I-NEXT: addw a0, a1, a2
+; RV64I-NEXT: slli a1, a3, 3
+; RV64I-NEXT: .LBB1_1: # %bb
+; RV64I-NEXT: # =>This Inner Loop Header: Depth=1
+; RV64I-NEXT: beq a0, a1, .LBB1_1
+; RV64I-NEXT: # %bb.2: # %bar
+; RV64I-NEXT: ret
+ br label %bb
+
+bb:
+ %a = icmp eq i32 %x, 0
+ %b = add i32 %y, %z
+ %c = select i1 %a, i32 8, i32 0
+ %d = icmp eq i32 %b, %c
+ br i1 %d, label %bb, label %bar
+
+bar:
+ ret i32 %b
+}
More information about the llvm-commits
mailing list