Skip to content

Commit dcd10d2

Browse files
author
git apple-llvm automerger
committed
Merge commit '141c3669bff7' from apple/main into swift/next
2 parents 27741d5 + 141c366 commit dcd10d2

13 files changed

+250
-7
lines changed

llvm/include/llvm/CodeGen/MIRYamlMapping.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ struct ScalarEnumerationTraits<TargetStackID::Value> {
348348
IO.enumCase(ID, "default", TargetStackID::Default);
349349
IO.enumCase(ID, "sgpr-spill", TargetStackID::SGPRSpill);
350350
IO.enumCase(ID, "scalable-vector", TargetStackID::ScalableVector);
351+
IO.enumCase(ID, "wasm-local", TargetStackID::WasmLocal);
351352
IO.enumCase(ID, "noalloc", TargetStackID::NoAlloc);
352353
}
353354
};

llvm/include/llvm/CodeGen/TargetFrameLowering.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ namespace llvm {
2424
class RegScavenger;
2525

2626
namespace TargetStackID {
27-
enum Value {
28-
Default = 0,
29-
SGPRSpill = 1,
30-
ScalableVector = 2,
31-
NoAlloc = 255
32-
};
27+
enum Value {
28+
Default = 0,
29+
SGPRSpill = 1,
30+
ScalableVector = 2,
31+
WasmLocal = 3,
32+
NoAlloc = 255
33+
};
3334
}
3435

3536
/// Information about stack frame layout on the target. It holds the direction

llvm/lib/Target/WebAssembly/Utils/WebAssemblyUtilities.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313

1414
#include "WebAssemblyUtilities.h"
1515
#include "WebAssemblyMachineFunctionInfo.h"
16+
#include "WebAssemblySubtarget.h"
17+
#include "llvm/CodeGen/Analysis.h"
18+
#include "llvm/CodeGen/MachineFrameInfo.h"
1619
#include "llvm/CodeGen/MachineInstr.h"
1720
#include "llvm/CodeGen/MachineLoopInfo.h"
21+
#include "llvm/IR/Instructions.h"
1822
#include "llvm/MC/MCContext.h"
1923
using namespace llvm;
2024

@@ -24,6 +28,50 @@ const char *const WebAssembly::StdTerminateFn = "_ZSt9terminatev";
2428
const char *const WebAssembly::PersonalityWrapperFn =
2529
"_Unwind_Wasm_CallPersonality";
2630

31+
// In an ideal world, when objects are added to the MachineFrameInfo by
32+
// FunctionLoweringInfo::set, we could somehow hook into target-specific code to
33+
// ensure they are assigned the right stack ID. However there isn't a hook that
34+
// runs between then and DAG building time, though, so instead we hoist stack
35+
// objects lazily when they are first used, and comprehensively after the DAG is
36+
// built via the PreprocessISelDAG hook, called by the
37+
// SelectionDAGISel::runOnMachineFunction. We have to do it in two places
38+
// because we want to do it while building the selection DAG for uses of alloca,
39+
// but not all alloca instructions are used so we have to follow up afterwards.
40+
Optional<unsigned> WebAssembly::getLocalForStackObject(MachineFunction &MF,
41+
int FrameIndex) {
42+
auto &MFI = MF.getFrameInfo();
43+
44+
// If already hoisted to a local, done.
45+
if (MFI.getStackID(FrameIndex) == TargetStackID::WasmLocal)
46+
return static_cast<unsigned>(MFI.getObjectOffset(FrameIndex));
47+
48+
// If not allocated in the object address space, this object will be in
49+
// linear memory.
50+
const AllocaInst *AI = MFI.getObjectAllocation(FrameIndex);
51+
if (!AI || !isWasmVarAddressSpace(AI->getType()->getAddressSpace()))
52+
return None;
53+
54+
// Otherwise, allocate this object in the named value stack, outside of linear
55+
// memory.
56+
SmallVector<EVT, 4> ValueVTs;
57+
const WebAssemblyTargetLowering &TLI =
58+
*MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
59+
WebAssemblyFunctionInfo *FuncInfo = MF.getInfo<WebAssemblyFunctionInfo>();
60+
ComputeValueVTs(TLI, MF.getDataLayout(), AI->getAllocatedType(), ValueVTs);
61+
MFI.setStackID(FrameIndex, TargetStackID::WasmLocal);
62+
// Abuse SP offset to record the index of the first local in the object.
63+
unsigned Local = FuncInfo->getParams().size() + FuncInfo->getLocals().size();
64+
MFI.setObjectOffset(FrameIndex, Local);
65+
// Allocate WebAssembly locals for each non-aggregate component of the
66+
// allocation.
67+
for (EVT ValueVT : ValueVTs)
68+
FuncInfo->addLocal(ValueVT.getSimpleVT());
69+
// Abuse object size to record number of WebAssembly locals allocated to
70+
// this object.
71+
MFI.setObjectSize(FrameIndex, ValueVTs.size());
72+
return static_cast<unsigned>(Local);
73+
}
74+
2775
/// Test whether MI is a child of some other node in an expression tree.
2876
bool WebAssembly::isChild(const MachineInstr &MI,
2977
const WebAssemblyFunctionInfo &MFI) {

llvm/lib/Target/WebAssembly/Utils/WebAssemblyUtilities.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
#ifndef LLVM_LIB_TARGET_WEBASSEMBLY_UTILS_WEBASSEMBLYUTILITIES_H
1616
#define LLVM_LIB_TARGET_WEBASSEMBLY_UTILS_WEBASSEMBLYUTILITIES_H
1717

18+
#include "llvm/ADT/Optional.h"
19+
1820
namespace llvm {
1921

2022
class MachineBasicBlock;
2123
class MachineInstr;
24+
class MachineFunction;
2225
class MachineOperand;
2326
class MCContext;
2427
class MCSymbolWasm;
@@ -48,6 +51,10 @@ inline bool isValidAddressSpace(unsigned AS) {
4851
return isDefaultAddressSpace(AS) || isWasmVarAddressSpace(AS);
4952
}
5053

54+
// Returns the index of the WebAssembly local to which the stack object
55+
// FrameIndex in MF should be allocated, or None.
56+
Optional<unsigned> getLocalForStackObject(MachineFunction &MF, int FrameIndex);
57+
5158
bool isChild(const MachineInstr &MI, const WebAssemblyFunctionInfo &MFI);
5259
bool mayThrow(const MachineInstr &MI);
5360

llvm/lib/Target/WebAssembly/WebAssemblyExplicitLocals.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,10 @@ bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) {
239239
Changed = true;
240240
}
241241

242-
// Start assigning local numbers after the last parameter.
242+
// Start assigning local numbers after the last parameter and after any
243+
// already-assigned locals.
243244
unsigned CurLocal = static_cast<unsigned>(MFI.getParams().size());
245+
CurLocal += static_cast<unsigned>(MFI.getLocals().size());
244246

245247
// Precompute the set of registers that are unused, so that we can insert
246248
// drops to their defs.

llvm/lib/Target/WebAssembly/WebAssemblyFrameLowering.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,16 @@ void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF,
314314
writeSPToGlobal(SPReg, MF, MBB, InsertPt, DL);
315315
}
316316

317+
bool WebAssemblyFrameLowering::isSupportedStackID(
318+
TargetStackID::Value ID) const {
319+
// Use the Object stack for WebAssembly locals which can only be accessed
320+
// by name, not via an address in linear memory.
321+
if (ID == TargetStackID::WasmLocal)
322+
return true;
323+
324+
return TargetFrameLowering::isSupportedStackID(ID);
325+
}
326+
317327
TargetFrameLowering::DwarfFrameBase
318328
WebAssemblyFrameLowering::getDwarfFrameBase(const MachineFunction &MF) const {
319329
DwarfFrameBase Loc;

llvm/lib/Target/WebAssembly/WebAssemblyFrameLowering.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class WebAssemblyFrameLowering final : public TargetFrameLowering {
4343

4444
bool hasFP(const MachineFunction &MF) const override;
4545
bool hasReservedCallFrame(const MachineFunction &MF) const override;
46+
bool isSupportedStackID(TargetStackID::Value ID) const override;
4647
DwarfFrameBase getDwarfFrameBase(const MachineFunction &MF) const override;
4748

4849
bool needsPrologForEH(const MachineFunction &MF) const;

llvm/lib/Target/WebAssembly/WebAssemblyISD.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ HANDLE_NODETYPE(CALL)
1717
HANDLE_NODETYPE(RET_CALL)
1818
HANDLE_NODETYPE(RETURN)
1919
HANDLE_NODETYPE(ARGUMENT)
20+
HANDLE_NODETYPE(LOCAL_GET)
21+
HANDLE_NODETYPE(LOCAL_SET)
2022
// A wrapper node for TargetExternalSymbol, TargetGlobalAddress, and MCSymbol
2123
HANDLE_NODETYPE(Wrapper)
2224
// A special wapper used in PIC code for __memory_base/__table_base relative

llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
15+
#include "Utils/WebAssemblyUtilities.h"
1516
#include "WebAssembly.h"
1617
#include "WebAssemblyTargetMachine.h"
18+
#include "llvm/CodeGen/MachineFrameInfo.h"
1719
#include "llvm/CodeGen/SelectionDAGISel.h"
1820
#include "llvm/IR/DiagnosticInfo.h"
1921
#include "llvm/IR/Function.h" // To access function attributes.
@@ -56,6 +58,8 @@ class WebAssemblyDAGToDAGISel final : public SelectionDAGISel {
5658
return SelectionDAGISel::runOnMachineFunction(MF);
5759
}
5860

61+
void PreprocessISelDAG() override;
62+
5963
void Select(SDNode *Node) override;
6064

6165
bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
@@ -69,6 +73,18 @@ class WebAssemblyDAGToDAGISel final : public SelectionDAGISel {
6973
};
7074
} // end anonymous namespace
7175

76+
void WebAssemblyDAGToDAGISel::PreprocessISelDAG() {
77+
// Stack objects that should be allocated to locals are hoisted to WebAssembly
78+
// locals when they are first used. However for those without uses, we hoist
79+
// them here. It would be nice if there were some hook to do this when they
80+
// are added to the MachineFrameInfo, but that's not the case right now.
81+
MachineFrameInfo &FrameInfo = MF->getFrameInfo();
82+
for (int Idx = 0; Idx < FrameInfo.getObjectIndexEnd(); Idx++)
83+
WebAssembly::getLocalForStackObject(*MF, Idx);
84+
85+
SelectionDAGISel::PreprocessISelDAG();
86+
}
87+
7288
void WebAssemblyDAGToDAGISel::Select(SDNode *Node) {
7389
// If we have a custom node, we already have selected!
7490
if (Node->isMachineOpcode()) {

llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,15 @@ static bool IsWebAssemblyGlobal(SDValue Op) {
12761276
return false;
12771277
}
12781278

1279+
static Optional<unsigned> IsWebAssemblyLocal(SDValue Op, SelectionDAG &DAG) {
1280+
const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op);
1281+
if (!FI)
1282+
return None;
1283+
1284+
auto &MF = DAG.getMachineFunction();
1285+
return WebAssembly::getLocalForStackObject(MF, FI->getIndex());
1286+
}
1287+
12791288
SDValue WebAssemblyTargetLowering::LowerStore(SDValue Op,
12801289
SelectionDAG &DAG) const {
12811290
SDLoc DL(Op);
@@ -1295,6 +1304,17 @@ SDValue WebAssemblyTargetLowering::LowerStore(SDValue Op,
12951304
SN->getMemoryVT(), SN->getMemOperand());
12961305
}
12971306

1307+
if (Optional<unsigned> Local = IsWebAssemblyLocal(Base, DAG)) {
1308+
if (!Offset->isUndef())
1309+
report_fatal_error("unexpected offset when storing to webassembly local",
1310+
false);
1311+
1312+
SDValue Idx = DAG.getTargetConstant(*Local, Base, MVT::i32);
1313+
SDVTList Tys = DAG.getVTList(MVT::Other); // The chain.
1314+
SDValue Ops[] = {SN->getChain(), Idx, Value};
1315+
return DAG.getNode(WebAssemblyISD::LOCAL_SET, DL, Tys, Ops);
1316+
}
1317+
12981318
return Op;
12991319
}
13001320

@@ -1316,6 +1336,20 @@ SDValue WebAssemblyTargetLowering::LowerLoad(SDValue Op,
13161336
LN->getMemoryVT(), LN->getMemOperand());
13171337
}
13181338

1339+
if (Optional<unsigned> Local = IsWebAssemblyLocal(Base, DAG)) {
1340+
if (!Offset->isUndef())
1341+
report_fatal_error(
1342+
"unexpected offset when loading from webassembly local", false);
1343+
1344+
SDValue Idx = DAG.getTargetConstant(*Local, Base, MVT::i32);
1345+
EVT LocalVT = LN->getValueType(0);
1346+
SDValue LocalGet = DAG.getNode(WebAssemblyISD::LOCAL_GET, DL, LocalVT,
1347+
{LN->getChain(), Idx});
1348+
SDValue Result = DAG.getMergeValues({LocalGet, LN->getChain()}, DL);
1349+
assert(Result->getNumValues() == 2 && "Loads must carry a chain!");
1350+
return Result;
1351+
}
1352+
13191353
return Op;
13201354
}
13211355

llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.td

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ def SDT_WebAssemblyCallSeqEnd :
7272
SDCallSeqEnd<[SDTCisVT<0, iPTR>, SDTCisVT<1, iPTR>]>;
7373
def SDT_WebAssemblyBrTable : SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>;
7474
def SDT_WebAssemblyArgument : SDTypeProfile<1, 1, [SDTCisVT<1, i32>]>;
75+
def SDT_WebAssemblyLocalGet : SDTypeProfile<1, 1, [SDTCisVT<1, i32>]>;
76+
def SDT_WebAssemblyLocalSet : SDTypeProfile<0, 2, [SDTCisVT<0, i32>]>;
7577
def SDT_WebAssemblyReturn : SDTypeProfile<0, -1, []>;
7678
def SDT_WebAssemblyWrapper : SDTypeProfile<1, 1, [SDTCisSameAs<0, 1>,
7779
SDTCisPtrTy<0>]>;
@@ -114,6 +116,12 @@ def WebAssemblyglobal_get :
114116
def WebAssemblyglobal_set :
115117
SDNode<"WebAssemblyISD::GLOBAL_SET", SDT_WebAssemblyGlobalSet,
116118
[SDNPHasChain, SDNPMayStore, SDNPMemOperand]>;
119+
def WebAssemblylocal_get :
120+
SDNode<"WebAssemblyISD::LOCAL_GET", SDT_WebAssemblyLocalGet,
121+
[SDNPHasChain, SDNPMayLoad]>;
122+
def WebAssemblylocal_set :
123+
SDNode<"WebAssemblyISD::LOCAL_SET", SDT_WebAssemblyLocalSet,
124+
[SDNPHasChain, SDNPMayStore]>;
117125

118126
//===----------------------------------------------------------------------===//
119127
// WebAssembly-specific Operands.
@@ -332,6 +340,10 @@ multiclass LOCAL<WebAssemblyRegClass rc, Operand global_op> {
332340
def : Pat<(WebAssemblyglobal_set
333341
vt:$src, (WebAssemblywrapper tglobaladdr:$addr)),
334342
(!cast<NI>("GLOBAL_SET_" # rc) tglobaladdr:$addr, vt:$src)>;
343+
def : Pat<(vt (WebAssemblylocal_get (i32 timm:$local))),
344+
(!cast<NI>("LOCAL_GET_" # rc) timm:$local)>;
345+
def : Pat<(WebAssemblylocal_set timm:$local, vt:$src),
346+
(!cast<NI>("LOCAL_SET_" # rc) timm:$local, vt:$src)>;
335347
}
336348
}
337349
defm "" : LOCAL<I32, global_op32>;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
; RUN: llc -mtriple=wasm32-unknown-unknown -asm-verbose=false < %s | FileCheck %s --check-prefix=CHECKCG
2+
; RUN: llc -mtriple=wasm32-unknown-unknown -stop-after=finalize-isel < %s | FileCheck %s --check-prefix=CHECKISEL
3+
4+
%f32_cell = type float addrspace(1)*
5+
6+
; CHECKISEL-LABEL: name: ir_local_f32
7+
; CHECKISEL: stack:
8+
; CHECKISEL: id: 0, name: retval, type: default, offset: 1, size: 1, alignment: 4,
9+
; CHECKISEL-NEXT: stack-id: wasm-local
10+
11+
; CHECKCG-LABEL: ir_local_f32:
12+
; CHECKCG-NEXT: .functype ir_local_f32 (f32) -> (f32)
13+
; CHECKCG-NEXT: .local f32
14+
; CHECKCG-NEXT: local.get 0
15+
; CHECKCG-NEXT: local.set 1
16+
17+
define float @ir_local_f32(float %arg) {
18+
%retval = alloca float, addrspace(1)
19+
store float %arg, %f32_cell %retval
20+
%reloaded = load float, %f32_cell %retval
21+
ret float %reloaded
22+
}

0 commit comments

Comments
 (0)