Skip to content

Commit 2e15040

Browse files
committed
[WebAssembly] Update MC for bulk memory
Summary: Rename MemoryIndex to InitFlags and implement logic for determining data segment layout in ObjectYAML and MC. Also adds a "passive" flag for the .section assembler directive although this cannot be assembled yet because the assembler does not support data sections. Reviewers: sbc100, aardappel, aheejin, dschuff Subscribers: jgravelle-google, hiraditya, sunfish, rupprecht, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D57938 llvm-svn: 354397
1 parent 8eade09 commit 2e15040

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+263
-119
lines changed

lld/test/wasm/call-indirect.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ define void @call_ptr(i64 (i64)* %arg) {
154154
; CHECK-NEXT: - Type: DATA
155155
; CHECK-NEXT: Segments:
156156
; CHECK-NEXT: - SectionOffset: 7
157-
; CHECK-NEXT: MemoryIndex: 0
157+
; CHECK-NEXT: InitFlags: 0
158158
; CHECK-NEXT: Offset:
159159
; CHECK-NEXT: Opcode: I32_CONST
160160
; CHECK-NEXT: Value: 1024

lld/test/wasm/comdats.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ entry:
9292
; CHECK-NEXT: - Type: DATA
9393
; CHECK-NEXT: Segments:
9494
; CHECK-NEXT: - SectionOffset: 7
95-
; CHECK-NEXT: MemoryIndex: 0
95+
; CHECK-NEXT: InitFlags: 0
9696
; CHECK-NEXT: Offset:
9797
; CHECK-NEXT: Opcode: I32_CONST
9898
; CHECK-NEXT: Value: 1024

lld/test/wasm/data-layout.ll

+12-12
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,20 @@ target triple = "wasm32-unknown-unknown"
3737
; CHECK: - Type: DATA
3838
; CHECK-NEXT: Segments:
3939
; CHECK-NEXT: - SectionOffset: 7
40-
; CHECK-NEXT: MemoryIndex: 0
40+
; CHECK-NEXT: InitFlags: 0
4141
; CHECK-NEXT: Offset:
4242
; CHECK-NEXT: Opcode: I32_CONST
4343
; CHECK-NEXT: Value: 1024
4444
; CHECK-NEXT: Content: '0100000000000000000000000000000003000000000000002804000024040000'
4545
; CHECK-NEXT: - SectionOffset: 45
46-
; CHECK-NEXT: MemoryIndex: 0
46+
; CHECK-NEXT: InitFlags: 0
4747
; CHECK-NEXT: Offset:
4848
; CHECK-NEXT: Opcode: I32_CONST
4949
; CHECK-NEXT: Value: 1056
5050
; CHECK-NEXT: Content: '0000000000000000'
5151
; CHECK-NEXT: - SectionOffset: 59
52-
; CHECK-NEXT: MemoryIndex: 0
53-
; CHECK-NEXT: Offset:
52+
; CHECK-NEXT: InitFlags: 0
53+
; CHECK-NEXT: Offset:
5454
; CHECK-NEXT: Opcode: I32_CONST
5555
; CHECK-NEXT: Value: 1064
5656
; CHECK-NEXT: Content: 68656C6C6F0A00
@@ -93,37 +93,37 @@ target triple = "wasm32-unknown-unknown"
9393
; RELOC-NEXT: Addend: 4
9494
; RELOC-NEXT: Segments:
9595
; RELOC-NEXT: - SectionOffset: 6
96-
; RELOC-NEXT: MemoryIndex: 0
96+
; RELOC-NEXT: InitFlags: 0
9797
; RELOC-NEXT: Offset:
9898
; RELOC-NEXT: Opcode: I32_CONST
9999
; RELOC-NEXT: Value: 0
100100
; RELOC-NEXT: Content: '01000000'
101101
; RELOC-NEXT: - SectionOffset: 15
102-
; RELOC-NEXT: MemoryIndex: 0
102+
; RELOC-NEXT: InitFlags: 0
103103
; RELOC-NEXT: Offset:
104104
; RELOC-NEXT: Opcode: I32_CONST
105105
; RELOC-NEXT: Value: 16
106106
; RELOC-NEXT: Content: '03000000'
107107
; RELOC-NEXT: - SectionOffset: 24
108-
; RELOC-NEXT: MemoryIndex: 0
108+
; RELOC-NEXT: InitFlags: 0
109109
; RELOC-NEXT: Offset:
110110
; RELOC-NEXT: Opcode: I32_CONST
111111
; RELOC-NEXT: Value: 24
112112
; RELOC-NEXT: Content: '28000000'
113113
; RELOC-NEXT: - SectionOffset: 33
114-
; RELOC-NEXT: MemoryIndex: 0
115-
; RELOC-NEXT: Offset:
114+
; RELOC-NEXT: InitFlags: 0
115+
; RELOC-NEXT: Offset:
116116
; RELOC-NEXT: Opcode: I32_CONST
117117
; RELOC-NEXT: Value: 28
118118
; RELOC-NEXT: Content: '0000000000000000'
119119
; RELOC-NEXT: - SectionOffset: 46
120-
; RELOC-NEXT: MemoryIndex: 0
121-
; RELOC-NEXT: Offset:
120+
; RELOC-NEXT: InitFlags: 0
121+
; RELOC-NEXT: Offset:
122122
; RELOC-NEXT: Opcode: I32_CONST
123123
; RELOC-NEXT: Value: 36
124124
; RELOC-NEXT: Content: '20000000'
125125
; RELOC-NEXT: - SectionOffset: 55
126-
; RELOC-NEXT: MemoryIndex: 0
126+
; RELOC-NEXT: InitFlags: 0
127127
; RELOC-NEXT: Offset:
128128
; RELOC-NEXT: Opcode: I32_CONST
129129
; RELOC-NEXT: Value: 40

lld/test/wasm/gc-sections.ll

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ entry:
6666
; CHECK: - Type: DATA
6767
; CHECK-NEXT: Segments:
6868
; CHECK-NEXT: - SectionOffset: 7
69-
; CHECK-NEXT: MemoryIndex: 0
70-
; CHECK-NEXT: Offset:
69+
; CHECK-NEXT: InitFlags: 0
70+
; CHECK-NEXT: Offset:
7171
; CHECK-NEXT: Opcode: I32_CONST
7272
; CHECK-NEXT: Value: 1024
7373
; CHECK-NEXT: Content: '02000000'
@@ -129,8 +129,8 @@ entry:
129129
; NO-GC: - Type: DATA
130130
; NO-GC-NEXT: Segments:
131131
; NO-GC-NEXT: - SectionOffset: 7
132-
; NO-GC-NEXT: MemoryIndex: 0
133-
; NO-GC-NEXT: Offset:
132+
; NO-GC-NEXT: InitFlags: 0
133+
; NO-GC-NEXT: Offset:
134134
; NO-GC-NEXT: Opcode: I32_CONST
135135
; NO-GC-NEXT: Value: 1024
136136
; NO-GC-NEXT: Content: '010000000000000002000000'

lld/test/wasm/init-fini.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
; RUN: llc -filetype=obj -o %t.o %s
2-
; RUN: llc -filetype=obj %S/Inputs/global-ctor-dtor.ll -o %t.global-ctor-dtor.o
1+
; RUN: llc -filetype=obj -thread-model=single -o %t.o %s
2+
; RUN: llc -filetype=obj -thread-model=single %S/Inputs/global-ctor-dtor.ll -o %t.global-ctor-dtor.o
33

44
target triple = "wasm32-unknown-unknown"
55

lld/test/wasm/local-symbols.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ entry:
101101
; CHECK-NEXT: - Type: DATA
102102
; CHECK-NEXT: Segments:
103103
; CHECK-NEXT: - SectionOffset: 7
104-
; CHECK-NEXT: MemoryIndex: 0
104+
; CHECK-NEXT: InitFlags: 0
105105
; CHECK-NEXT: Offset:
106106
; CHECK-NEXT: Opcode: I32_CONST
107107
; CHECK-NEXT: Value: 1024

lld/test/wasm/locals-duplicate.test

+4-4
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@
187187
; CHECK-NEXT: - Type: DATA
188188
; CHECK-NEXT: Segments:
189189
; CHECK-NEXT: - SectionOffset: 7
190-
; CHECK-NEXT: MemoryIndex: 0
190+
; CHECK-NEXT: InitFlags: 0
191191
; CHECK-NEXT: Offset:
192192
; CHECK-NEXT: Opcode: I32_CONST
193193
; CHECK-NEXT: Value: 1024
@@ -363,19 +363,19 @@
363363
; RELOC-NEXT: - Type: DATA
364364
; RELOC-NEXT: Segments:
365365
; RELOC-NEXT: - SectionOffset: 6
366-
; RELOC-NEXT: MemoryIndex: 0
366+
; RELOC-NEXT: InitFlags: 0
367367
; RELOC-NEXT: Offset:
368368
; RELOC-NEXT: Opcode: I32_CONST
369369
; RELOC-NEXT: Value: 0
370370
; RELOC-NEXT: Content: '0000000000000000'
371371
; RELOC-NEXT: - SectionOffset: 19
372-
; RELOC-NEXT: MemoryIndex: 0
372+
; RELOC-NEXT: InitFlags: 0
373373
; RELOC-NEXT: Offset:
374374
; RELOC-NEXT: Opcode: I32_CONST
375375
; RELOC-NEXT: Value: 8
376376
; RELOC-NEXT: Content: '0000000000000000'
377377
; RELOC-NEXT: - SectionOffset: 32
378-
; RELOC-NEXT: MemoryIndex: 0
378+
; RELOC-NEXT: InitFlags: 0
379379
; RELOC-NEXT: Offset:
380380
; RELOC-NEXT: Opcode: I32_CONST
381381
; RELOC-NEXT: Value: 16

lld/test/wasm/lto/used.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ return:
2828
; CHECK: - Type: DATA
2929
; CHECK-NEXT: Segments:
3030
; CHECK-NEXT: - SectionOffset: 7
31-
; CHECK-NEXT: MemoryIndex: 0
31+
; CHECK-NEXT: InitFlags: 0
3232
; CHECK-NEXT: Offset:
3333
; CHECK-NEXT: Opcode: I32_CONST
3434
; CHECK-NEXT: Value: 1024

lld/test/wasm/many-functions.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -802,13 +802,13 @@ entry:
802802
; CHECK-NEXT: - Type: DATA
803803
; CHECK-NEXT: Segments:
804804
; CHECK-NEXT: - SectionOffset: 6
805-
; CHECK-NEXT: MemoryIndex: 0
805+
; CHECK-NEXT: InitFlags: 0
806806
; CHECK-NEXT: Offset:
807807
; CHECK-NEXT: Opcode: I32_CONST
808808
; CHECK-NEXT: Value: 0
809809
; CHECK-NEXT: Content: '01000000'
810810
; CHECK-NEXT: - SectionOffset: 15
811-
; CHECK-NEXT: MemoryIndex: 0
811+
; CHECK-NEXT: InitFlags: 0
812812
; CHECK-NEXT: Offset:
813813
; CHECK-NEXT: Opcode: I32_CONST
814814
; CHECK-NEXT: Value: 4

lld/test/wasm/relocatable.ll

+6-6
Original file line numberDiff line numberDiff line change
@@ -120,37 +120,37 @@ entry:
120120
; CHECK-NEXT: Offset: 0x0000002D
121121
; CHECK-NEXT: Segments:
122122
; CHECK-NEXT: - SectionOffset: 6
123-
; CHECK-NEXT: MemoryIndex: 0
123+
; CHECK-NEXT: InitFlags: 0
124124
; CHECK-NEXT: Offset:
125125
; CHECK-NEXT: Opcode: I32_CONST
126126
; CHECK-NEXT: Value: 0
127127
; CHECK-NEXT: Content: 68656C6C6F0A00
128128
; CHECK-NEXT: - SectionOffset: 18
129-
; CHECK-NEXT: MemoryIndex: 0
129+
; CHECK-NEXT: InitFlags: 0
130130
; CHECK-NEXT: Offset:
131131
; CHECK-NEXT: Opcode: I32_CONST
132132
; CHECK-NEXT: Value: 8
133133
; CHECK-NEXT: Content: '01000000'
134134
; CHECK-NEXT: - SectionOffset: 27
135-
; CHECK-NEXT: MemoryIndex: 0
135+
; CHECK-NEXT: InitFlags: 0
136136
; CHECK-NEXT: Offset:
137137
; CHECK-NEXT: Opcode: I32_CONST
138138
; CHECK-NEXT: Value: 12
139139
; CHECK-NEXT: Content: '02000000'
140140
; CHECK-NEXT: - SectionOffset: 36
141-
; CHECK-NEXT: MemoryIndex: 0
141+
; CHECK-NEXT: InitFlags: 0
142142
; CHECK-NEXT: Offset:
143143
; CHECK-NEXT: Opcode: I32_CONST
144144
; CHECK-NEXT: Value: 16
145145
; CHECK-NEXT: Content: '03000000'
146146
; CHECK-NEXT: - SectionOffset: 45
147-
; CHECK-NEXT: MemoryIndex: 0
147+
; CHECK-NEXT: InitFlags: 0
148148
; CHECK-NEXT: Offset:
149149
; CHECK-NEXT: Opcode: I32_CONST
150150
; CHECK-NEXT: Value: 24
151151
; CHECK-NEXT: Content: '00000000'
152152
; CHECK-NEXT: - SectionOffset: 54
153-
; CHECK-NEXT: MemoryIndex: 0
153+
; CHECK-NEXT: InitFlags: 0
154154
; CHECK-NEXT: Offset:
155155
; CHECK-NEXT: Opcode: I32_CONST
156156
; CHECK-NEXT: Value: 28

lld/test/wasm/shared.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ declare void @func_external()
7575
; CHECK: - Type: DATA
7676
; CHECK-NEXT: Segments:
7777
; CHECK-NEXT: - SectionOffset: 6
78-
; CHECK-NEXT: MemoryIndex: 0
78+
; CHECK-NEXT: InitFlags: 0
7979
; CHECK-NEXT: Offset:
8080
; CHECK-NEXT: Opcode: GLOBAL_GET
8181
; CHECK-NEXT: Index: 1

lld/test/wasm/weak-symbols.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ entry:
118118
; CHECK-NEXT: - Type: DATA
119119
; CHECK-NEXT: Segments:
120120
; CHECK-NEXT: - SectionOffset: 7
121-
; CHECK-NEXT: MemoryIndex: 0
121+
; CHECK-NEXT: InitFlags: 0
122122
; CHECK-NEXT: Offset:
123123
; CHECK-NEXT: Opcode: I32_CONST
124124
; CHECK-NEXT: Value: 1024

llvm/include/llvm/BinaryFormat/Wasm.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,13 @@ struct WasmFunction {
131131
};
132132

133133
struct WasmDataSegment {
134-
uint32_t MemoryIndex;
135-
WasmInitExpr Offset;
134+
uint32_t InitFlags;
135+
uint32_t MemoryIndex; // present if InitFlags & WASM_SEGMENT_HAS_MEMINDEX
136+
WasmInitExpr Offset; // present if InitFlags & WASM_SEGMENT_IS_PASSIVE == 0
136137
ArrayRef<uint8_t> Content;
137138
StringRef Name; // from the "segment info" section
138139
uint32_t Alignment;
139-
uint32_t Flags;
140+
uint32_t LinkerFlags;
140141
uint32_t Comdat; // from the "comdat info" section
141142
};
142143

@@ -247,6 +248,11 @@ enum : unsigned {
247248
WASM_LIMITS_FLAG_IS_SHARED = 0x2,
248249
};
249250

251+
enum : unsigned {
252+
WASM_SEGMENT_IS_PASSIVE = 0x01,
253+
WASM_SEGMENT_HAS_MEMINDEX = 0x02,
254+
};
255+
250256
// Kind codes used in the custom "name" section
251257
enum : unsigned {
252258
WASM_NAMES_FUNCTION = 0x1,

llvm/include/llvm/MC/MCSectionWasm.h

+11
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class MCSectionWasm final : public MCSection {
4242
// segment
4343
uint32_t SegmentIndex = 0;
4444

45+
// Whether this data segment is passive
46+
bool IsPassive = false;
47+
4548
friend class MCContext;
4649
MCSectionWasm(StringRef Section, SectionKind K, const MCSymbolWasm *group,
4750
unsigned UniqueID, MCSymbol *Begin)
@@ -75,6 +78,14 @@ class MCSectionWasm final : public MCSection {
7578
uint32_t getSegmentIndex() const { return SegmentIndex; }
7679
void setSegmentIndex(uint32_t Index) { SegmentIndex = Index; }
7780

81+
bool getPassive() const {
82+
assert(isWasmData());
83+
return IsPassive;
84+
}
85+
void setPassive(bool V = true) {
86+
assert(isWasmData());
87+
IsPassive = V;
88+
}
7889
static bool classof(const MCSection *S) { return S->getVariant() == SV_Wasm; }
7990
};
8091

llvm/include/llvm/ObjectYAML/WasmYAML.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ struct Relocation {
111111
};
112112

113113
struct DataSegment {
114-
uint32_t MemoryIndex;
115114
uint32_t SectionOffset;
115+
uint32_t InitFlags;
116+
uint32_t MemoryIndex;
116117
wasm::WasmInitExpr Offset;
117118
yaml::BinaryRef Content;
118119
};

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -1693,8 +1693,14 @@ MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal(
16931693
Group = C->getName();
16941694
}
16951695

1696-
return getContext().getWasmSection(Name, Kind, Group,
1697-
MCContext::GenericSectionID);
1696+
MCSectionWasm* Section =
1697+
getContext().getWasmSection(Name, Kind, Group,
1698+
MCContext::GenericSectionID);
1699+
1700+
if (TM.Options.ThreadModel != ThreadModel::Single)
1701+
Section->setPassive();
1702+
1703+
return Section;
16981704
}
16991705

17001706
static MCSectionWasm *selectWasmSectionForGlobal(
@@ -1723,7 +1729,12 @@ static MCSectionWasm *selectWasmSectionForGlobal(
17231729
UniqueID = *NextUniqueID;
17241730
(*NextUniqueID)++;
17251731
}
1726-
return Ctx.getWasmSection(Name, Kind, Group, UniqueID);
1732+
1733+
MCSectionWasm* Section = Ctx.getWasmSection(Name, Kind, Group, UniqueID);
1734+
if (Section->isWasmData() && TM.Options.ThreadModel != ThreadModel::Single)
1735+
Section->setPassive();
1736+
1737+
return Section;
17271738
}
17281739

17291740
MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal(

llvm/lib/MC/MCParser/WasmAsmParser.cpp

+43-3
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,53 @@ class WasmAsmParser : public MCAsmParserExtension {
8181
return false;
8282
}
8383

84+
bool parseSectionFlags(StringRef FlagStr, bool &Passive) {
85+
SmallVector<StringRef, 2> Flags;
86+
// If there are no flags, keep Flags empty
87+
FlagStr.split(Flags, ",", -1, false);
88+
for (auto &Flag : Flags) {
89+
if (Flag == "passive")
90+
Passive = true;
91+
else
92+
return error("Expected section flags, instead got: ", Lexer->getTok());
93+
}
94+
return false;
95+
}
96+
8497
bool parseSectionDirective(StringRef, SMLoc) {
8598
StringRef Name;
8699
if (Parser->parseIdentifier(Name))
87100
return TokError("expected identifier in directive");
88-
// FIXME: currently requiring this very fixed format.
89-
if (expect(AsmToken::Comma, ",") || expect(AsmToken::String, "string") ||
90-
expect(AsmToken::Comma, ",") || expect(AsmToken::At, "@") ||
101+
102+
if (expect(AsmToken::Comma, ","))
103+
return true;
104+
105+
if (Lexer->isNot(AsmToken::String))
106+
return error("expected string in directive, instead got: ", Lexer->getTok());
107+
108+
SectionKind Kind = StringSwitch<SectionKind>(Name)
109+
.StartsWith(".data", SectionKind::getData())
110+
.StartsWith(".rodata", SectionKind::getReadOnly())
111+
.StartsWith(".text", SectionKind::getText())
112+
.StartsWith(".custom_section", SectionKind::getMetadata());
113+
114+
MCSectionWasm* Section = getContext().getWasmSection(Name, Kind);
115+
116+
// Update section flags if present in this .section directive
117+
bool Passive = false;
118+
if (parseSectionFlags(getTok().getStringContents(), Passive))
119+
return true;
120+
121+
if (Passive) {
122+
if (!Section->isWasmData())
123+
return Parser->Error(getTok().getLoc(),
124+
"Only data sections can be passive");
125+
Section->setPassive();
126+
}
127+
128+
Lex();
129+
130+
if (expect(AsmToken::Comma, ",") || expect(AsmToken::At, "@") ||
91131
expect(AsmToken::EndOfStatement, "eol"))
92132
return true;
93133
// This is done automatically by the assembler for text sections currently,

0 commit comments

Comments
 (0)