Skip to content

Commit 0436cf5

Browse files
committed
[LoongArch] Support parsing target specific flags for MIR
These hooks ensure that the LoongArch backend can serialize and parse MIR correctly. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D137482
1 parent 926acd2 commit 0436cf5

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

llvm/lib/Target/LoongArch/LoongArchInstrInfo.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,28 @@ bool LoongArchInstrInfo::reverseBranchCondition(
434434
Cond[0].setImm(getOppositeBranchOpc(Cond[0].getImm()));
435435
return false;
436436
}
437+
438+
std::pair<unsigned, unsigned>
439+
LoongArchInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
440+
return std::make_pair(TF, 0u);
441+
}
442+
443+
ArrayRef<std::pair<unsigned, const char *>>
444+
LoongArchInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
445+
using namespace LoongArchII;
446+
// TODO: Add more target flags.
447+
static const std::pair<unsigned, const char *> TargetFlags[] = {
448+
{MO_CALL, "loongarch-call"},
449+
{MO_CALL_PLT, "loongarch-call-plt"},
450+
{MO_PCREL_HI, "loongarch-pcrel-hi"},
451+
{MO_PCREL_LO, "loongarch-pcrel-lo"},
452+
{MO_GOT_PC_HI, "loongarch-got-pc-hi"},
453+
{MO_GOT_PC_LO, "loongarch-got-pc-lo"},
454+
{MO_LE_HI, "loongarch-le-hi"},
455+
{MO_LE_LO, "loongarch-le-lo"},
456+
{MO_IE_PC_HI, "loongarch-ie-pc-hi"},
457+
{MO_IE_PC_LO, "loongarch-ie-pc-lo"},
458+
{MO_LD_PC_HI, "loongarch-ld-pc-hi"},
459+
{MO_GD_PC_HI, "loongarch-gd-pc-hi"}};
460+
return makeArrayRef(TargetFlags);
461+
}

llvm/lib/Target/LoongArch/LoongArchInstrInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ class LoongArchInstrInfo : public LoongArchGenInstrInfo {
7474
bool
7575
reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
7676

77+
std::pair<unsigned, unsigned>
78+
decomposeMachineOperandsTargetFlags(unsigned TF) const override;
79+
80+
ArrayRef<std::pair<unsigned, const char *>>
81+
getSerializableDirectMachineOperandTargetFlags() const override;
82+
7783
protected:
7884
const LoongArchSubtarget &STI;
7985
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
; RUN: llc --mtriple=loongarch64 --stop-after loongarch-prera-expand-pseudo \
2+
; RUN: --relocation-model=pic %s -o %t.mir
3+
; RUN: llc --mtriple=loongarch64 --run-pass loongarch-prera-expand-pseudo \
4+
; RUN: %t.mir -o - | FileCheck %s
5+
6+
;; This tests the LoongArch-specific serialization and deserialization of
7+
;; `target-flags(...)`
8+
9+
@g_e = external global i32
10+
@g_i = internal global i32 0
11+
@t_un = external thread_local global i32
12+
@t_ld = external thread_local(localdynamic) global i32
13+
@t_ie = external thread_local(initialexec) global i32
14+
@t_le = external thread_local(localexec) global i32
15+
16+
declare void @callee1() nounwind
17+
declare dso_local void @callee2() nounwind
18+
19+
define void @caller() nounwind {
20+
; CHECK-LABEL: name: caller
21+
; CHECK: target-flags(loongarch-got-pc-hi) @g_e
22+
; CHECK-NEXT: target-flags(loongarch-got-pc-lo) @g_e
23+
; CHECK: target-flags(loongarch-pcrel-hi) @g_i
24+
; CHECK-NEXT: target-flags(loongarch-pcrel-lo) @g_i
25+
; CHECK: target-flags(loongarch-gd-pc-hi) @t_un
26+
; CHECK-NEXT: target-flags(loongarch-got-pc-lo) @t_un
27+
; CHECK: target-flags(loongarch-ld-pc-hi) @t_ld
28+
; CHECK-NEXT: target-flags(loongarch-got-pc-lo) @t_ld
29+
; CHECK: target-flags(loongarch-ie-pc-hi) @t_ie
30+
; CHECK-NEXT: target-flags(loongarch-ie-pc-lo) @t_ie
31+
; CHECK: target-flags(loongarch-le-hi) @t_le
32+
; CHECK-NEXT: target-flags(loongarch-le-lo) @t_le
33+
; CHECK: target-flags(loongarch-call-plt) @callee1
34+
; CHECK: target-flags(loongarch-call) @callee2
35+
%a = load volatile i32, ptr @g_e
36+
%b = load volatile i32, ptr @g_i
37+
%c = load volatile i32, ptr @t_un
38+
%d = load volatile i32, ptr @t_ld
39+
%e = load volatile i32, ptr @t_ie
40+
%f = load volatile i32, ptr @t_le
41+
call i32 @callee1()
42+
call i32 @callee2()
43+
ret void
44+
}

0 commit comments

Comments
 (0)