Skip to content

Commit aea0a6a

Browse files
toppercllvmbot
authored andcommitted
[RISCV] Don't crash in RISCVMergeBaseOffset if INLINE_ASM uses address register in a non-memory constraint. (#100790)
If the register is used by a non-memory constraint we should disable the fold. Otherwise, we may leave CommonOffset unassigned. Fixes #100779. (cherry picked from commit c901b73)
1 parent a4902a3 commit aea0a6a

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,16 @@ bool RISCVMergeBaseOffsetOpt::foldIntoMemoryOps(MachineInstr &Hi,
429429
NumOps = Flags.getNumOperandRegisters();
430430

431431
// Memory constraints have two operands.
432-
if (NumOps != 2 || !Flags.isMemKind())
432+
if (NumOps != 2 || !Flags.isMemKind()) {
433+
// If the register is used by something other than a memory contraint,
434+
// we should not fold.
435+
for (unsigned J = 0; J < NumOps; ++J) {
436+
const MachineOperand &MO = UseMI.getOperand(I + 1 + J);
437+
if (MO.isReg() && MO.getReg() == DestReg)
438+
return false;
439+
}
433440
continue;
441+
}
434442

435443
// We can't do this for constraint A because AMO instructions don't have
436444
// an immediate offset field.

llvm/test/CodeGen/RISCV/inline-asm-mem-constraint.ll

+50
Original file line numberDiff line numberDiff line change
@@ -2252,3 +2252,53 @@ label:
22522252
call void asm "lw zero, $0", "*A"(ptr elementtype(i32) getelementptr (i8, ptr blockaddress(@constraint_A_with_local_3, %label), i32 2000))
22532253
ret void
22542254
}
2255+
2256+
@_ZN5repro9MY_BUFFER17hb0f674501d5980a6E = external global <{ [16 x i8] }>
2257+
2258+
; Address is not used by a memory constraint.
2259+
define void @should_not_fold() {
2260+
; RV32I-LABEL: should_not_fold:
2261+
; RV32I: # %bb.0: # %start
2262+
; RV32I-NEXT: .cfi_def_cfa_offset 0
2263+
; RV32I-NEXT: lui a0, %hi(_ZN5repro9MY_BUFFER17hb0f674501d5980a6E)
2264+
; RV32I-NEXT: addi a0, a0, %lo(_ZN5repro9MY_BUFFER17hb0f674501d5980a6E)
2265+
; RV32I-NEXT: #APP
2266+
; RV32I-NEXT: ecall
2267+
; RV32I-NEXT: #NO_APP
2268+
; RV32I-NEXT: ret
2269+
;
2270+
; RV64I-LABEL: should_not_fold:
2271+
; RV64I: # %bb.0: # %start
2272+
; RV64I-NEXT: .cfi_def_cfa_offset 0
2273+
; RV64I-NEXT: lui a0, %hi(_ZN5repro9MY_BUFFER17hb0f674501d5980a6E)
2274+
; RV64I-NEXT: addi a0, a0, %lo(_ZN5repro9MY_BUFFER17hb0f674501d5980a6E)
2275+
; RV64I-NEXT: #APP
2276+
; RV64I-NEXT: ecall
2277+
; RV64I-NEXT: #NO_APP
2278+
; RV64I-NEXT: ret
2279+
;
2280+
; RV32I-MEDIUM-LABEL: should_not_fold:
2281+
; RV32I-MEDIUM: # %bb.0: # %start
2282+
; RV32I-MEDIUM-NEXT: .cfi_def_cfa_offset 0
2283+
; RV32I-MEDIUM-NEXT: .Lpcrel_hi39:
2284+
; RV32I-MEDIUM-NEXT: auipc a0, %pcrel_hi(_ZN5repro9MY_BUFFER17hb0f674501d5980a6E)
2285+
; RV32I-MEDIUM-NEXT: addi a0, a0, %pcrel_lo(.Lpcrel_hi39)
2286+
; RV32I-MEDIUM-NEXT: #APP
2287+
; RV32I-MEDIUM-NEXT: ecall
2288+
; RV32I-MEDIUM-NEXT: #NO_APP
2289+
; RV32I-MEDIUM-NEXT: ret
2290+
;
2291+
; RV64I-MEDIUM-LABEL: should_not_fold:
2292+
; RV64I-MEDIUM: # %bb.0: # %start
2293+
; RV64I-MEDIUM-NEXT: .cfi_def_cfa_offset 0
2294+
; RV64I-MEDIUM-NEXT: .Lpcrel_hi39:
2295+
; RV64I-MEDIUM-NEXT: auipc a0, %pcrel_hi(_ZN5repro9MY_BUFFER17hb0f674501d5980a6E)
2296+
; RV64I-MEDIUM-NEXT: addi a0, a0, %pcrel_lo(.Lpcrel_hi39)
2297+
; RV64I-MEDIUM-NEXT: #APP
2298+
; RV64I-MEDIUM-NEXT: ecall
2299+
; RV64I-MEDIUM-NEXT: #NO_APP
2300+
; RV64I-MEDIUM-NEXT: ret
2301+
start:
2302+
%0 = tail call ptr asm sideeffect alignstack "ecall", "=&{x10},0,~{vtype},~{vl},~{vxsat},~{vxrm},~{memory}"(ptr @_ZN5repro9MY_BUFFER17hb0f674501d5980a6E)
2303+
ret void
2304+
}

0 commit comments

Comments
 (0)