llvm.org GIT mirror llvm / 99e2e27
IR: Allow metadata attachments on declarations, and fix lazy loaded metadata issue with globals. This change is motivated by an upcoming change to the metadata representation used for CFI. The indirect function call checker needs type information for external function declarations in order to correctly generate jump table entries for such declarations. We currently associate such type information with declarations using a global metadata node, but I plan [1] to move all such metadata to global object attachments. In bitcode, metadata attachments for function declarations appear in the global metadata block. This seems reasonable to me because I expect metadata attachments on declarations to be uncommon. In the long term I'd also expect this to be the case for CFI, because we'd want to use some specialized bitcode format for this metadata that could be read as part of the ThinLTO thin-link phase, which would mean that it would not appear in the global metadata block. To solve the lazy loaded metadata issue I was seeing with D20147, I use the same bitcode representation for metadata attachments for global variables as I do for function declarations. Since there's a use case for metadata attachments in the global metadata block, we might as well use that representation for global variables as well, at least until we have a mechanism for lazy loading global variables. In the assembly format, the metadata attachments appear after the "declare" keyword in order to avoid a parsing ambiguity. [1] http://lists.llvm.org/pipermail/llvm-dev/2016-June/100462.html Differential Revision: http://reviews.llvm.org/D21052 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273336 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 4 years ago
15 changed file(s) with 170 addition(s) and 102 deletion(s). Raw diff Collapse all Expand all
860860 be one ``GCNAME`` record for each garbage collector name referenced in function
861861 ``gc`` attributes within the module. These records can be referenced by 1-based
862862 index in the *gc* fields of ``FUNCTION`` records.
863
864 MODULE_CODE_GLOBALVAR_ATTACHMENT Record
865 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
866
867 ``[GLOBALVAR_ATTACHMENT, valueid, n x [id, mdnode]]``
868
869 The ``GLOBALVAR_ATTACHMENT`` record (code 19) describes the metadata
870 attachments for a global variable. The ``valueid`` is the value index for
871 the global variable, and the remaining fields are pairs of metadata name
872 indices and metadata node indices.
873863
874864 .. _PARAMATTR_BLOCK:
875865
112112
113113 // IFUNC: [ifunc value type, addrspace, resolver val#, linkage, visibility]
114114 MODULE_CODE_IFUNC = 18,
115
116 // GLOBALVAR_ATTACHMENT: [valueid, n x [id, mdnode]]
117 MODULE_CODE_GLOBALVAR_ATTACHMENT = 19,
118115 };
119116
120117 /// PARAMATTR blocks have code for defining a parameter attribute set.
259256 METADATA_MACRO = 33, // [distinct, macinfo, line, name, value]
260257 METADATA_MACRO_FILE = 34, // [distinct, macinfo, line, file, ...]
261258 METADATA_STRINGS = 35, // [count, offset] blob([lengths][chars])
259 METADATA_GLOBAL_DECL_ATTACHMENT = 36, // [valueid, n x [id, mdnode]]
262260 };
263261
264262 // The constants block (CONSTANTS_BLOCK_ID) describes emission for each
396396 assert(Lex.getKind() == lltok::kw_declare);
397397 Lex.Lex();
398398
399 std::vector> MDs;
400 while (Lex.getKind() == lltok::MetadataVar) {
401 unsigned MDK;
402 MDNode *N;
403 if (ParseMetadataAttachment(MDK, N))
404 return true;
405 MDs.push_back({MDK, N});
406 }
407
399408 Function *F;
400 return ParseFunctionHeader(F, false);
409 if (ParseFunctionHeader(F, false))
410 return true;
411 for (auto &MD : MDs)
412 F->addMetadata(MD.first, *MD.second);
413 return false;
401414 }
402415
403416 /// toplevelentity
26912691 parseMetadataStrings(Record, Blob, NextMetadataNo))
26922692 return EC;
26932693 break;
2694 case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: {
2695 if (Record.size() % 2 == 0)
2696 return error("Invalid record");
2697 unsigned ValueID = Record[0];
2698 if (ValueID >= ValueList.size())
2699 return error("Invalid record");
2700 if (auto *GO = dyn_cast(ValueList[ValueID]))
2701 parseGlobalObjectAttachment(*GO, ArrayRef(Record).slice(1));
2702 break;
2703 }
26942704 case bitc::METADATA_KIND: {
26952705 // Support older bitcode files that had METADATA_KIND records in a
26962706 // block with METADATA_BLOCK_ID.
38373847 NewGV->setComdat(reinterpret_cast(1));
38383848 }
38393849
3840 break;
3841 }
3842 case bitc::MODULE_CODE_GLOBALVAR_ATTACHMENT: {
3843 if (Record.size() % 2 == 0)
3844 return error("Invalid record");
3845 unsigned ValueID = Record[0];
3846 if (ValueID >= ValueList.size())
3847 return error("Invalid record");
3848 if (auto *GV = dyn_cast(ValueList[ValueID]))
3849 parseGlobalObjectAttachment(*GV, ArrayRef(Record).slice(1));
38503850 break;
38513851 }
38523852 // FUNCTION: [type, callingconv, isproto, linkage, paramattr,
226226 void writeGlobalVariableMetadataAttachment(const GlobalVariable &GV);
227227 void pushGlobalMetadataAttachment(SmallVectorImpl &Record,
228228 const GlobalObject &GO);
229 void writeModuleMetadataStore();
229 void writeModuleMetadataKinds();
230230 void writeOperandBundleTags();
231231 void writeConstants(unsigned FirstVal, unsigned LastVal, bool isGlobal);
232232 void writeModuleConstants();
18311831 writeMetadataStrings(VE.getMDStrings(), Record);
18321832 writeMetadataRecords(VE.getNonMDStrings(), Record);
18331833 writeNamedMetadata(Record);
1834
1835 auto AddDeclAttachedMetadata = [&](const GlobalObject &GO) {
1836 SmallVector Record;
1837 Record.push_back(VE.getValueID(&GO));
1838 pushGlobalMetadataAttachment(Record, GO);
1839 Stream.EmitRecord(bitc::METADATA_GLOBAL_DECL_ATTACHMENT, Record);
1840 };
1841 for (const Function &F : M)
1842 if (F.isDeclaration() && F.hasMetadata())
1843 AddDeclAttachedMetadata(F);
1844 // FIXME: Only store metadata for declarations here, and move data for global
1845 // variable definitions to a separate block (PR28134).
1846 for (const GlobalVariable &GV : M.globals())
1847 if (GV.hasMetadata())
1848 AddDeclAttachedMetadata(GV);
1849
18341850 Stream.ExitBlock();
18351851 }
18361852
18911907 Stream.ExitBlock();
18921908 }
18931909
1894 void ModuleBitcodeWriter::writeModuleMetadataStore() {
1910 void ModuleBitcodeWriter::writeModuleMetadataKinds() {
18951911 SmallVector Record;
18961912
18971913 // Write metadata kinds
35923608 // Emit constants.
35933609 writeModuleConstants();
35943610
3611 // Emit metadata kind names.
3612 writeModuleMetadataKinds();
3613
35953614 // Emit metadata.
35963615 writeModuleMetadata();
3597
3598 // Emit metadata.
3599 writeModuleMetadataStore();
36003616
36013617 // Emit module-level use-lists.
36023618 if (VE.shouldPreserveUseListOrder())
36173633
36183634 writeValueSymbolTable(M.getValueSymbolTable(),
36193635 /* IsModuleLevel */ true, &FunctionToBitcodeIndex);
3620
3621 for (const GlobalVariable &GV : M.globals())
3622 if (GV.hasMetadata()) {
3623 SmallVector Record;
3624 Record.push_back(VE.getValueID(&GV));
3625 pushGlobalMetadataAttachment(Record, GV);
3626 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR_ATTACHMENT, Record);
3627 }
36283636
36293637 if (GenerateHash) {
36303638 writeModuleHash(BlockStartPos);
347347 MDs.clear();
348348 GV.getAllMetadata(MDs);
349349 for (const auto &I : MDs)
350 EnumerateMetadata(&GV, I.second);
350 // FIXME: Pass GV to EnumerateMetadata and arrange for the bitcode writer
351 // to write metadata to the global variable's own metadata block
352 // (PR28134).
353 EnumerateMetadata(nullptr, I.second);
351354 }
352355
353356 // Enumerate types used by function bodies and argument lists.
359362 MDs.clear();
360363 F.getAllMetadata(MDs);
361364 for (const auto &I : MDs)
362 EnumerateMetadata(&F, I.second);
365 EnumerateMetadata(F.isDeclaration() ? nullptr : &F, I.second);
363366
364367 for (const BasicBlock &BB : F)
365368 for (const Instruction &I : BB) {
529532 EnumerateMetadata(nullptr, MD->getOperand(i));
530533 }
531534
532 unsigned ValueEnumerator::getMetadataGlobalID(const GlobalObject *GO) const {
533 return GO ? getValueID(GO) + 1 : 0;
534 }
535
536 void ValueEnumerator::EnumerateMetadata(const GlobalObject *GO,
537 const Metadata *MD) {
538 EnumerateMetadata(getMetadataGlobalID(GO), MD);
535 unsigned ValueEnumerator::getMetadataFunctionID(const Function *F) const {
536 return F ? getValueID(F) + 1 : 0;
537 }
538
539 void ValueEnumerator::EnumerateMetadata(const Function *F, const Metadata *MD) {
540 EnumerateMetadata(getMetadataFunctionID(F), MD);
539541 }
540542
541543 void ValueEnumerator::EnumerateFunctionLocalMetadata(
542544 const Function &F, const LocalAsMetadata *Local) {
543 EnumerateFunctionLocalMetadata(getMetadataGlobalID(&F), Local);
545 EnumerateFunctionLocalMetadata(getMetadataFunctionID(&F), Local);
544546 }
545547
546548 void ValueEnumerator::dropFunctionFromMetadata(
254254 /// it's an \a MDNode.
255255 const MDNode *enumerateMetadataImpl(unsigned F, const Metadata *MD);
256256
257 unsigned getMetadataGlobalID(const GlobalObject *GO) const;
257 unsigned getMetadataFunctionID(const Function *F) const;
258258
259259 /// Enumerate reachable metadata in (almost) post-order.
260260 ///
271271 /// \a organizeMetadata() will later partition distinct nodes ahead of
272272 /// uniqued ones.
273273 ///{
274 void EnumerateMetadata(const GlobalObject *GO, const Metadata *MD);
274 void EnumerateMetadata(const Function *F, const Metadata *MD);
275275 void EnumerateMetadata(unsigned F, const Metadata *MD);
276276 ///}
277277
26152615 Out << "; Function Attrs: " << AttrStr << '\n';
26162616 }
26172617
2618 if (F->isDeclaration())
2619 Out << "declare ";
2620 else
2618 Machine.incorporateFunction(F);
2619
2620 if (F->isDeclaration()) {
2621 Out << "declare";
2622 SmallVector, 4> MDs;
2623 F->getAllMetadata(MDs);
2624 printMetadataAttachments(MDs, " ");
2625 Out << ' ';
2626 } else
26212627 Out << "define ";
26222628
26232629 Out << getLinkagePrintName(F->getLinkage());
26372643 Out << ' ';
26382644 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
26392645 Out << '(';
2640 Machine.incorporateFunction(F);
26412646
26422647 // Loop over the arguments, printing them...
26432648 if (F->isDeclaration() && !IsForDebug) {
26972702 writeOperand(F->getPersonalityFn(), /*PrintType=*/true);
26982703 }
26992704
2700 SmallVector, 4> MDs;
2701 F->getAllMetadata(MDs);
2702 printMetadataAttachments(MDs, " ");
2703
27042705 if (F->isDeclaration()) {
27052706 Out << '\n';
27062707 } else {
2708 SmallVector, 4> MDs;
2709 F->getAllMetadata(MDs);
2710 printMetadataAttachments(MDs, " ");
2711
27072712 Out << " {";
27082713 // Output all of the function's basic blocks.
27092714 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
19551955 Assert(MDs.empty(), "unmaterialized function cannot have metadata", &F,
19561956 MDs.empty() ? nullptr : MDs.front().second);
19571957 } else if (F.isDeclaration()) {
1958 Assert(MDs.empty(), "function without a body cannot have metadata", &F,
1959 MDs.empty() ? nullptr : MDs.front().second);
1958 for (const auto &I : MDs) {
1959 AssertDI(I.first != LLVMContext::MD_dbg,
1960 "function declaration may not have a !dbg attachment", &F);
1961 Assert(I.first != LLVMContext::MD_prof,
1962 "function declaration may not have a !prof attachment", &F);
1963
1964 // Verify the metadata itself.
1965 visitMDNode(*I.second);
1966 }
19601967 Assert(!F.hasPersonalityFn(),
19611968 "Function declaration shouldn't have a personality routine", &F);
19621969 } else {
0 ; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s
1 ; RUN: llvm-as < %s | llvm-dis -materialize-metadata | FileCheck %s
2
3 ; CHECK: @foo = external global i32, !foo !0
4 @foo = external global i32, !foo !0
5
6 ; CHECK: declare !bar !1 void @bar()
7 declare !bar !1 void @bar()
8
9 !0 = distinct !{}
10 !1 = distinct !{}
None ; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s
0 ; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck --check-prefix=CHECK --check-prefix=CHECK-UNMAT %s
1 ; RUN: llvm-as < %s | llvm-dis -materialize-metadata | FileCheck --check-prefix=CHECK-UNMAT %s
12 ; RUN: verify-uselistorder %s
23
3 ; CHECK: @global = global i32 0, !foo [[M2:![0-9]+]], !foo [[M3:![0-9]+]], !baz [[M3]]
4 ; CHECK-UNMAT: @global = global i32 0, !foo [[M2:![0-9]+]], !foo [[M3:![0-9]+]], !baz [[M3]]
45 @global = global i32 0, !foo !2, !foo !3, !baz !3
56
67 ; CHECK-LABEL: @test
3132 unreachable, !\34\32abc !4
3233 }
3334
34 ; CHECK: [[M2]] = distinct !{}
35 ; CHECK: [[M3]] = distinct !{}
35 ; CHECK-UNMAT: [[M2]] = distinct !{}
36 ; CHECK-UNMAT: [[M3]] = distinct !{}
3637 ; CHECK: [[M0]] = !DILocation
3738 ; CHECK: [[M1]] = distinct !DISubprogram
3839 ; CHECK: [[M4]] = distinct !{}
0 ; RUN: not llvm-as %s -disable-output 2>&1 | FileCheck %s
11
2 define void @foo() !dbg !4 {
2 ; CHECK: function declaration may not have a !dbg attachment
3 declare !dbg !4 void @f1()
4
5 define void @f2() !dbg !4 {
36 unreachable
47 }
58
69 ; CHECK: function must have a single !dbg attachment
7 define void @foo2() !dbg !4 !dbg !4 {
10 define void @f3() !dbg !4 !dbg !4 {
811 unreachable
912 }
1013
0 ; RUN: not llvm-as %s -disable-output 2>&1 | FileCheck %s
11
2 define void @foo() !prof !0 {
2 ; CHECK: function declaration may not have a !prof attachment
3 declare !prof !0 void @f1()
4
5 define void @f2() !prof !0 {
36 unreachable
47 }
58
69 ; CHECK: function must have a single !prof attachment
7 define void @foo2() !prof !0 !prof !0 {
10 define void @f3() !prof !0 !prof !0 {
811 unreachable
912 }
1013
2626 #include "llvm/IR/Type.h"
2727 #include "llvm/Support/CommandLine.h"
2828 #include "llvm/Support/DataStream.h"
29 #include "llvm/Support/Error.h"
2930 #include "llvm/Support/FileSystem.h"
3031 #include "llvm/Support/FormattedStream.h"
3132 #include "llvm/Support/ManagedStatic.h"
5758 "preserve-ll-uselistorder",
5859 cl::desc("Preserve use-list order when writing LLVM assembly."),
5960 cl::init(false), cl::Hidden);
61
62 static cl::opt
63 MaterializeMetadata("materialize-metadata",
64 cl::desc("Load module without materializing metadata, "
65 "then materialize only the metadata"));
6066
6167 namespace {
6268
131137 exit(1);
132138 }
133139
134 int main(int argc, char **argv) {
135 // Print a stack trace if we signal out.
136 sys::PrintStackTraceOnErrorSignal(argv[0]);
137 PrettyStackTraceProgram X(argc, argv);
138
139 LLVMContext Context;
140 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
141
142 Context.setDiagnosticHandler(diagnosticHandler, argv[0]);
143
144 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
145
146 std::string ErrorMessage;
147 std::unique_ptr M;
148
149 // Use the bitcode streaming interface
150 std::unique_ptr Streamer =
151 getDataFileStreamer(InputFilename, &ErrorMessage);
152 if (Streamer) {
140 static Expected> openInputFile(LLVMContext &Context) {
141 if (MaterializeMetadata) {
142 ErrorOr> MBOrErr =
143 MemoryBuffer::getFileOrSTDIN(InputFilename);
144 if (!MBOrErr)
145 return errorCodeToError(MBOrErr.getError());
146 ErrorOr> MOrErr =
147 getLazyBitcodeModule(std::move(*MBOrErr), Context,
148 /*ShouldLazyLoadMetadata=*/true);
149 if (!MOrErr)
150 return errorCodeToError(MOrErr.getError());
151 (*MOrErr)->materializeMetadata();
152 return std::move(*MOrErr);
153 } else {
154 std::string ErrorMessage;
155 std::unique_ptr Streamer =
156 getDataFileStreamer(InputFilename, &ErrorMessage);
157 if (!Streamer)
158 return make_error(ErrorMessage, inconvertibleErrorCode());
153159 std::string DisplayFilename;
154160 if (InputFilename == "-")
155161 DisplayFilename = "";
157163 DisplayFilename = InputFilename;
158164 ErrorOr> MOrErr =
159165 getStreamedBitcodeModule(DisplayFilename, std::move(Streamer), Context);
160 M = std::move(*MOrErr);
161 M->materializeAll();
162 } else {
163 errs() << argv[0] << ": " << ErrorMessage << '\n';
166 (*MOrErr)->materializeAll();
167 return std::move(*MOrErr);
168 }
169 }
170
171 int main(int argc, char **argv) {
172 // Print a stack trace if we signal out.
173 sys::PrintStackTraceOnErrorSignal(argv[0]);
174 PrettyStackTraceProgram X(argc, argv);
175
176 LLVMContext Context;
177 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
178
179 Context.setDiagnosticHandler(diagnosticHandler, argv[0]);
180
181 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
182
183 Expected> MOrErr = openInputFile(Context);
184 if (!MOrErr) {
185 handleAllErrors(MOrErr.takeError(), [&](ErrorInfoBase &EIB) {
186 errs() << argv[0] << ": ";
187 EIB.log(errs());
188 errs() << '\n';
189 });
164190 return 1;
165191 }
192 std::unique_ptr M = std::move(*MOrErr);
166193
167194 // Just use stdout. We won't actually print anything on it.
168195 if (DontPrint)
22592259 TEST_F(FunctionAttachmentTest, Verifier) {
22602260 Function *F = getFunction("foo");
22612261 F->setMetadata("attach", getTuple());
2262
2263 // Confirm this has no body.
2264 ASSERT_TRUE(F->empty());
2265
2266 // Functions without a body cannot have metadata attachments (they also can't
2267 // be verified directly, so check that the module fails to verify).
2268 EXPECT_TRUE(verifyModule(*F->getParent()));
2269
2270 // Nor can materializable functions.
22712262 F->setIsMaterializable(true);
2272 EXPECT_TRUE(verifyModule(*F->getParent()));
2273
2274 // Functions with a body can.
2263
2264 // Confirm this is materializable.
2265 ASSERT_TRUE(F->isMaterializable());
2266
2267 // Materializable functions cannot have metadata attachments.
2268 EXPECT_TRUE(verifyFunction(*F));
2269
2270 // Function declarations can.
22752271 F->setIsMaterializable(false);
2272 EXPECT_FALSE(verifyModule(*F->getParent()));
2273 EXPECT_FALSE(verifyFunction(*F));
2274
2275 // So can definitions.
22762276 (void)new UnreachableInst(Context, BasicBlock::Create(Context, "bb", F));
22772277 EXPECT_FALSE(verifyModule(*F->getParent()));
22782278 EXPECT_FALSE(verifyFunction(*F));