Skip to content

Commit 519b26a

Browse files
Sightemadriweb
authored andcommitted
Z80: Fix GlobalISel crashes for LLVM v16 compatibility
- Fix tablegen combiner conflicts by removing fp rule groups - Use IncomingValueAssigner for formal arguments (was incorrectly OutgoingValueAssigner) - Add createMachineFunctionInfo override - Update Z80MachineFunctionInfo with required v16 constructors and clone method - Fix matchConstant to check both outer and inner optionals - Add BitWidth > 0 guards to prevent failures on zero width APInts - Auto update test expectations, all 17 Z80 regression tests now pass MIPatternMatch.h should be upstreamed as it is a generic LLVM header and we really should not be touching it
1 parent 4aa1525 commit 519b26a

File tree

11 files changed

+1822
-1801
lines changed

11 files changed

+1822
-1801
lines changed

llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ template <>
8282
inline std::optional<APInt> matchConstant<APInt>(Register Reg,
8383
const MachineRegisterInfo &MRI) {
8484
const auto& Val = matchConstant<std::optional<ValueAndVReg>>(Reg, MRI);
85-
if (Val.has_value())
85+
if (Val.has_value() && Val.value().has_value())
8686
return { Val.value()->Value };
8787
return { std::nullopt };
8888
}
@@ -98,7 +98,7 @@ template <>
9898
inline std::optional<int64_t> matchConstant<int64_t>(Register Reg,
9999
const MachineRegisterInfo &MRI) {
100100
auto Val = matchConstant<APInt>(Reg, MRI);
101-
if (Val && Val->getBitWidth() <= 64)
101+
if (Val && Val->getBitWidth() > 0 && Val->getBitWidth() <= 64)
102102
return Val->getSExtValue();
103103
return std::nullopt;
104104
}
@@ -107,7 +107,7 @@ template <>
107107
inline std::optional<uint64_t> matchConstant<uint64_t>(Register Reg,
108108
const MachineRegisterInfo &MRI) {
109109
auto Val = matchConstant<APInt>(Reg, MRI);
110-
if (Val && Val->getBitWidth() <= 64)
110+
if (Val && Val->getBitWidth() > 0 && Val->getBitWidth() <= 64)
111111
return Val->getSExtValue();
112112
return std::nullopt;
113113
}
@@ -116,7 +116,7 @@ template <>
116116
inline std::optional<unsigned char> matchConstant<unsigned char>(Register Reg,
117117
const MachineRegisterInfo &MRI) {
118118
auto Val = matchConstant<APInt>(Reg, MRI);
119-
if (Val && Val->getBitWidth() <= 64)
119+
if (Val && Val->getBitWidth() > 0 && Val->getBitWidth() <= 64)
120120
return Val->getSExtValue();
121121
return std::nullopt;
122122
}
@@ -125,7 +125,7 @@ template <>
125125
inline std::optional<unsigned int> matchConstant<unsigned int>(Register Reg,
126126
const MachineRegisterInfo &MRI) {
127127
auto Val = matchConstant<APInt>(Reg, MRI);
128-
if (Val && Val->getBitWidth() <= 64)
128+
if (Val && Val->getBitWidth() > 0 && Val->getBitWidth() <= 64)
129129
return Val->getSExtValue();
130130
return std::nullopt;
131131
}
@@ -134,7 +134,7 @@ template <>
134134
inline std::optional<bool> matchConstant<bool>(Register Reg,
135135
const MachineRegisterInfo &MRI) {
136136
auto Val = matchConstant<APInt>(Reg, MRI);
137-
if (Val && Val->getBitWidth() <= 64)
137+
if (Val && Val->getBitWidth() > 0 && Val->getBitWidth() <= 64)
138138
return Val->getSExtValue();
139139
return std::nullopt;
140140
}

llvm/lib/Target/Z80/GISel/Z80CallLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ bool Z80CallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder,
762762
if (!MBB.empty())
763763
MIRBuilder.setInstr(*MBB.begin());
764764

765-
OutgoingValueAssigner Assigner(CC_Z80);
765+
IncomingValueAssigner Assigner(CC_Z80);
766766
FormalArgHandler Handler(MIRBuilder, MRI);
767767
if (!determineAndHandleAssignments(Handler, Assigner, SplitArgs, MIRBuilder,
768768
F.getCallingConv(), F.isVarArg()))

llvm/lib/Target/Z80/Z80Combine.td

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,40 @@ def flip_setcc_cond : GICombineRule<
2525
[{ return matchFlipSetCCCond(*${mi}, MRI, ${matchinfo}); }]),
2626
(apply [{ applyFlipSetCCCond(*${mi}, B, Observer, *${matchinfo}); }])>;
2727

28+
// identity combines (excluding FP related fneg_fneg_fold)
29+
def z80_identity_combines : GICombineGroup<[select_same_val, right_identity_zero,
30+
binop_same_val, binop_left_to_zero,
31+
binop_right_to_zero, p2i_to_i2p,
32+
i2p_to_p2i, anyext_trunc_fold,
33+
right_identity_one,
34+
add_sub_reg, buildvector_identity_fold,
35+
trunc_buildvector_fold,
36+
trunc_lshr_buildvector_fold,
37+
bitcast_bitcast_fold]>;
38+
39+
// const combines (excluding FP related constant_fp_op, combine_minmax_nan)
40+
def z80_const_combines : GICombineGroup<[const_ptradd_to_i2p,
41+
overlapping_and, mulo_by_2, mulo_by_0,
42+
addo_by_0, adde_to_addo]>;
43+
2844
def Z80PreLegalizerCombinerHelper
2945
: GICombinerHelper<"Z80GenPreLegalizerCombinerHelper", [
3046
and_ext, and_or_disjoint_mask, combine_ext_or_trunc, combine_identity,
31-
combine_trunc_shift, constant_fold, const_combines, div_rem_to_divrem,
32-
ext_ext_fold, /*fabs_fabs_fold,*/ fabs_fneg_fold, flip_cmp_cond,
47+
combine_trunc_shift, constant_fold, z80_const_combines, div_rem_to_divrem,
48+
ext_ext_fold, flip_cmp_cond,
3349
flip_setcc_cond, form_bitfield_extract, funnel_shift,
3450
funnel_shift_combines, hoist_logic_op_with_same_opcode_hands,
35-
identity_combines, intdiv_combines, known_bits_simplifications,
51+
z80_identity_combines, intdiv_combines, known_bits_simplifications,
3652
load_or_combine, lower_is_power_of_two, merge_unmerge, mulh_combines,
3753
narrow_count_zext, narrow_icmp, narrow_load, narrow_op, not_cmp_fold,
3854
opt_brcond_by_inverting_cond, or_to_add, phi_combines,
3955
ptr_add_const_immed, ptr_add_global_immed, ptr_add_immed_chain,
4056
ptr_add_with_zero, reassocs, reassoc_fold_consts,
41-
redundant_neg_operands, select_combines, sext_to_zext,
57+
select_combines, sext_to_zext,
4258
shift_immed_chain, shift_of_shifted_logic_chain, shl_ashr_to_sext_inreg,
4359
simplify_add_to_sub, simplify_const, simplify_icmp_bool,
4460
simplify_known_const, split_brcond, sub_const_to_add_neg,
45-
trivial_combines, truncstore_merge, trunc_ext_fold, /*trunc_shl,*/
61+
trivial_combines, truncstore_merge, trunc_ext_fold,
4662
undef_combines, unmerge_cst, unmerge_dead_to_trunc, unmerge_merge,
4763
unmerge_zext_to_zext, width_reduction_combines,
4864
xor_of_and_with_same_reg,

llvm/lib/Target/Z80/Z80MachineFunctionInfo.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,16 @@ class Z80MachineFunctionInfo : public MachineFunctionInfo {
5252
public:
5353
Z80MachineFunctionInfo() = default;
5454

55-
explicit Z80MachineFunctionInfo(MachineFunction &MF) {}
55+
Z80MachineFunctionInfo(const Function &F, const TargetSubtargetInfo *STI) {}
56+
57+
Z80MachineFunctionInfo(const Z80MachineFunctionInfo &) = default;
58+
59+
MachineFunctionInfo *
60+
clone(BumpPtrAllocator &Allocator, MachineFunction &DestMF,
61+
const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
62+
const override {
63+
return DestMF.cloneInfo<Z80MachineFunctionInfo>(*this);
64+
}
5665

5766
unsigned getArgFrameSize() const { return ArgFrameSize; }
5867
void setArgFrameSize(unsigned Bytes) { ArgFrameSize = Bytes; }

llvm/lib/Target/Z80/Z80TargetMachine.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "Z80TargetMachine.h"
1515
#include "TargetInfo/Z80TargetInfo.h"
1616
#include "Z80.h"
17+
#include "Z80MachineFunctionInfo.h"
1718
#include "Z80Subtarget.h"
1819
#include "Z80TargetObjectFile.h"
1920
#include "llvm/ADT/SmallString.h"
@@ -165,6 +166,13 @@ TargetPassConfig *Z80TargetMachine::createPassConfig(PassManagerBase &PM) {
165166
return new Z80PassConfig(*this, PM);
166167
}
167168

169+
MachineFunctionInfo *Z80TargetMachine::createMachineFunctionInfo(
170+
BumpPtrAllocator &Allocator, const Function &F,
171+
const TargetSubtargetInfo *STI) const {
172+
return Z80MachineFunctionInfo::create<Z80MachineFunctionInfo>(Allocator, F,
173+
STI);
174+
}
175+
168176
bool Z80PassConfig::addIRTranslator() {
169177
addPass(new IRTranslator);
170178
return false;

llvm/lib/Target/Z80/Z80TargetMachine.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class Z80TargetMachine : public LLVMTargetMachine {
3636
TargetLoweringObjectFile *getObjFileLowering() const override {
3737
return TLOF.get();
3838
}
39+
40+
MachineFunctionInfo *
41+
createMachineFunctionInfo(BumpPtrAllocator &Allocator, const Function &F,
42+
const TargetSubtargetInfo *STI) const override;
3943
};
4044

4145
} // end namespace llvm

0 commit comments

Comments
 (0)