Skip to content

Commit 141f533

Browse files
[WebAssembly] Emit __llvm_covmap and __llvm_covfun as custom sections
This patch makes `__llvm_covmap` and `__llvm_covfun` as custom sections in the wasm object file because they will not be referenced at runtime but just used by `llvm-cov` post-processing tools. The same approach is used in the ELF object file to emit them without `SHF_ALLOC` flag. Those sections have their associated comdat group symbols, which are section symbols (not data symbols) in the wasm object file. This patch also sets the symbol type explicitly not to hit an assertion in the `WasmObjectWriter::writeOneObject` ("data symbols must live in a data section").
1 parent 5939422 commit 141f533

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2171,7 +2171,11 @@ MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal(
21712171
// This could be avoided if all data segements (the wasm sense) were
21722172
// represented as their own sections (in the llvm sense).
21732173
// TODO(sbc): https://github.com/WebAssembly/tool-conventions/issues/138
2174-
if (Name == ".llvmcmd" || Name == ".llvmbc")
2174+
if (Name == getInstrProfSectionName(IPSK_covmap, Triple::Wasm,
2175+
/*AddSegmentInfo=*/false) ||
2176+
Name == getInstrProfSectionName(IPSK_covfun, Triple::Wasm,
2177+
/*AddSegmentInfo=*/false) ||
2178+
Name == ".llvmbc" || Name == ".llvmcmd")
21752179
Kind = SectionKind::getMetadata();
21762180

21772181
StringRef Group = "";

llvm/lib/MC/MCContext.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,11 @@ MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K,
757757
if (!Group.isTriviallyEmpty() && !Group.str().empty()) {
758758
GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
759759
GroupSym->setComdat(true);
760+
if (K.isMetadata() && !GroupSym->getType().has_value()) {
761+
// Comdat group symbol associated with a custom section is a section
762+
// symbol (not a data symbol).
763+
GroupSym->setType(wasm::WASM_SYMBOL_TYPE_SECTION);
764+
}
760765
}
761766

762767
return getWasmSection(Section, K, Flags, GroupSym, UniqueID);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
; RUN: llc < %s --filetype=obj | obj2yaml | FileCheck %s
2+
3+
target triple = "wasm32-unknown-unknown"
4+
5+
$__covrec_A = comdat any
6+
$__covrec_B = comdat any
7+
8+
@__covrec_A = linkonce_odr hidden constant <{ i64, i32, i64, i64, [4 x i8] }> <{
9+
i64 -1978722966671112904,
10+
i32 4,
11+
i64 0,
12+
i64 -8102528905418564625,
13+
[4 x i8] c"\01\01\04\11"
14+
}>, section "__llvm_covfun", comdat, align 8
15+
@__covrec_B = linkonce_odr hidden constant <{ i64, i32, i64, i64, [4 x i8] }> <{
16+
i64 8006510647218728891,
17+
i32 9,
18+
i64 0,
19+
i64 -8102528905418564625,
20+
[4 x i8] c"\01\01\00\01"
21+
}>, section "__llvm_covfun", comdat, align 8
22+
@__llvm_coverage_mapping = private constant { { i32, i32, i32, i32 }, [4 x i8] } {
23+
{ i32, i32, i32, i32 } { i32 0, i32 87, i32 0, i32 5 },
24+
[4 x i8] c"\01\01\00\02"
25+
}, section "__llvm_covmap", align 8
26+
27+
; CHECK: - Type: CUSTOM
28+
; CHECK-NEXT: Name: __llvm_covfun
29+
; CHECK-NEXT: Payload: 3845A90EF2298AE4040000000000000000000000EF1B31BAE3088E8F01010411
30+
; CHECK-NEXT: - Type: CUSTOM
31+
; CHECK-NEXT: Name: __llvm_covfun
32+
; CHECK-NEXT: Payload: BBEFDA6903D71C6F090000000000000000000000EF1B31BAE3088E8F01010001
33+
; CHECK-NEXT: - Type: CUSTOM
34+
; CHECK-NEXT: Name: __llvm_covmap
35+
; CHECK-NEXT: Payload: '0000000057000000000000000500000001010002'
36+
; CHECK-NEXT: - Type: CUSTOM
37+
; CHECK-NEXT: Name: linking
38+
; CHECK-NEXT: Version: 2
39+
; CHECK-NEXT: Comdats:
40+
; CHECK-NEXT: - Name: __covrec_A
41+
; CHECK-NEXT: Entries:
42+
; CHECK-NEXT: - Kind: SECTION
43+
; CHECK-NEXT: Index: 1
44+
; CHECK-NEXT: - Name: __covrec_B
45+
; CHECK-NEXT: Entries:
46+
; CHECK-NEXT: - Kind: SECTION
47+
; CHECK-NEXT: Index: 2

0 commit comments

Comments
 (0)