[GlobalISel] Change widenScalar of G_FCONSTANT to mutate into G_CONSTANT.

Widening a G_FCONSTANT by extending and then generating G_FPTRUNC doesn't produce
the same result all the time. Instead, we can just transform it to a G_CONSTANT
of the same bit pattern and truncate using a plain G_TRUNC instead.

Fixes https://github.com/llvm/llvm-project/issues/56454

Differential Revision: https://reviews.llvm.org/D129743
This commit is contained in:
Amara Emerson 2022-07-14 00:53:59 -07:00
parent 0a92e0728c
commit d4f84df0a0
3 changed files with 48 additions and 29 deletions

View File

@ -2393,30 +2393,14 @@ LegalizerHelper::widenScalar(MachineInstr &MI, unsigned TypeIdx, LLT WideTy) {
return Legalized;
}
case TargetOpcode::G_FCONSTANT: {
// To avoid changing the bits of the constant due to extension to a larger
// type and then using G_FPTRUNC, we simply convert to a G_CONSTANT.
MachineOperand &SrcMO = MI.getOperand(1);
LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext();
APFloat Val = SrcMO.getFPImm()->getValueAPF();
bool LosesInfo;
switch (WideTy.getSizeInBits()) {
case 32:
Val.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
&LosesInfo);
break;
case 64:
Val.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven,
&LosesInfo);
break;
default:
return UnableToLegalize;
}
assert(!LosesInfo && "extend should always be lossless");
Observer.changingInstr(MI);
SrcMO.setFPImm(ConstantFP::get(Ctx, Val));
widenScalarDst(MI, WideTy, 0, TargetOpcode::G_FPTRUNC);
Observer.changedInstr(MI);
APInt Val = SrcMO.getFPImm()->getValueAPF().bitcastToAPInt();
MIRBuilder.setInstrAndDebugLoc(MI);
auto IntCst = MIRBuilder.buildConstant(MI.getOperand(0).getReg(), Val);
widenScalarDst(*IntCst, WideTy, 0, TargetOpcode::G_TRUNC);
MI.eraseFromParent();
return Legalized;
}
case TargetOpcode::G_IMPLICIT_DEF: {

View File

@ -48,10 +48,8 @@ body: |
; CHECK-NEXT: $w0 = COPY [[C]](s32)
; CHECK-NEXT: [[C1:%[0-9]+]]:_(s64) = G_FCONSTANT double 2.000000e+00
; CHECK-NEXT: $x0 = COPY [[C1]](s64)
; CHECK-NEXT: [[C2:%[0-9]+]]:_(s32) = G_FCONSTANT float 0.000000e+00
; CHECK-NEXT: [[FPTRUNC:%[0-9]+]]:_(s16) = G_FPTRUNC [[C2]](s32)
; CHECK-NEXT: [[ANYEXT:%[0-9]+]]:_(s32) = G_ANYEXT [[FPTRUNC]](s16)
; CHECK-NEXT: $w0 = COPY [[ANYEXT]](s32)
; CHECK-NEXT: [[C2:%[0-9]+]]:_(s32) = G_CONSTANT i32 0
; CHECK-NEXT: $w0 = COPY [[C2]](s32)
%0:_(s32) = G_FCONSTANT float 1.0
$w0 = COPY %0
%1:_(s64) = G_FCONSTANT double 2.0

View File

@ -8,8 +8,7 @@ tracksRegLiveness: true
body: |
bb.0:
; NO-FP16-LABEL: name: fp16
; NO-FP16: [[C:%[0-9]+]]:_(s32) = G_FCONSTANT float 0.000000e+00
; NO-FP16-NEXT: %cst:_(s16) = G_FPTRUNC [[C]](s32)
; NO-FP16: %cst:_(s16) = G_CONSTANT i16 0
; NO-FP16-NEXT: $h0 = COPY %cst(s16)
; NO-FP16-NEXT: RET_ReallyLR implicit $h0
; FP16-LABEL: name: fp16
@ -19,4 +18,42 @@ body: |
%cst:_(s16) = G_FCONSTANT half 0.0
$h0 = COPY %cst
RET_ReallyLR implicit $h0
...
---
name: fp16_non_zero
tracksRegLiveness: true
body: |
bb.0:
; NO-FP16-LABEL: name: fp16_non_zero
; NO-FP16: %cst:_(s16) = G_CONSTANT i16 16384
; NO-FP16-NEXT: $h0 = COPY %cst(s16)
; NO-FP16-NEXT: RET_ReallyLR implicit $h0
; FP16-LABEL: name: fp16_non_zero
; FP16: %cst:_(s16) = G_FCONSTANT half 0xH4000
; FP16-NEXT: $h0 = COPY %cst(s16)
; FP16-NEXT: RET_ReallyLR implicit $h0
%cst:_(s16) = G_FCONSTANT half 2.0
$h0 = COPY %cst
RET_ReallyLR implicit $h0
...
---
name: nan
tracksRegLiveness: true
body: |
bb.1.entry:
; NO-FP16-LABEL: name: nan
; NO-FP16: %cst:_(s16) = G_CONSTANT i16 31745
; NO-FP16-NEXT: %ext:_(s32) = G_FPEXT %cst(s16)
; NO-FP16-NEXT: $w0 = COPY %ext(s32)
; NO-FP16-NEXT: RET_ReallyLR implicit $w0
; FP16-LABEL: name: nan
; FP16: %cst:_(s16) = G_FCONSTANT half 0xH7C01
; FP16-NEXT: %ext:_(s32) = G_FPEXT %cst(s16)
; FP16-NEXT: $w0 = COPY %ext(s32)
; FP16-NEXT: RET_ReallyLR implicit $w0
%cst:_(s16) = G_FCONSTANT half 0xH7C01
%ext:_(s32) = G_FPEXT %cst(s16)
$w0 = COPY %ext(s32)
RET_ReallyLR implicit $w0
...