Skip to content

Commit 5a52e6d

Browse files
committed
Modernize the error handling of the Materialize function.
llvm-svn: 220600
1 parent 2813f49 commit 5a52e6d

File tree

12 files changed

+40
-37
lines changed

12 files changed

+40
-37
lines changed

llvm/include/llvm/IR/GVMaterializer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class GVMaterializer {
3838

3939
/// Make sure the given GlobalValue is fully read.
4040
///
41-
virtual std::error_code Materialize(GlobalValue *GV) = 0;
41+
virtual std::error_code materialize(GlobalValue *GV) = 0;
4242

4343
/// If the given GlobalValue is read in, and if the GVMaterializer supports
4444
/// it, release the memory for the GV, and set it up to be materialized

llvm/include/llvm/IR/GlobalValue.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "llvm/IR/Constant.h"
2222
#include "llvm/IR/DerivedTypes.h"
2323

24+
#include <system_error>
25+
2426
namespace llvm {
2527

2628
class Comdat;
@@ -311,7 +313,7 @@ class GlobalValue : public Constant {
311313
/// Make sure this GlobalValue is fully read. If the module is corrupt, this
312314
/// returns true and fills in the optional string with information about the
313315
/// problem. If successful, this returns false.
314-
bool Materialize(std::string *ErrInfo = nullptr);
316+
std::error_code materialize();
315317

316318
/// If this GlobalValue is read in, and if the GVMaterializer supports it,
317319
/// release the memory for the function, and set it up to be materialized

llvm/include/llvm/IR/Module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ class Module {
476476
/// Make sure the GlobalValue is fully read. If the module is corrupt, this
477477
/// returns true and fills in the optional string with information about the
478478
/// problem. If successful, this returns false.
479-
bool Materialize(GlobalValue *GV, std::string *ErrInfo = nullptr);
479+
std::error_code materialize(GlobalValue *GV);
480480
/// If the GlobalValue is read in, and if the GVMaterializer supports it,
481481
/// release the memory for the function, and set it up to be materialized
482482
/// lazily. If !isDematerializable(), this method is a noop.

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ std::error_code BitcodeReader::materializeForwardReferencedFunctions() {
5656
return Error(BitcodeError::NeverResolvedFunctionFromBlockAddress);
5757

5858
// Try to materialize F.
59-
if (std::error_code EC = Materialize(F))
59+
if (std::error_code EC = materialize(F))
6060
return EC;
6161
}
6262
assert(BasicBlockFwdRefs.empty() && "Function missing from queue");
@@ -3282,7 +3282,7 @@ std::error_code BitcodeReader::FindFunctionInStream(
32823282

32833283
void BitcodeReader::releaseBuffer() { Buffer.release(); }
32843284

3285-
std::error_code BitcodeReader::Materialize(GlobalValue *GV) {
3285+
std::error_code BitcodeReader::materialize(GlobalValue *GV) {
32863286
Function *F = dyn_cast<Function>(GV);
32873287
// If it's not a function or is already material, ignore the request.
32883288
if (!F || !F->isMaterializable())
@@ -3358,7 +3358,7 @@ std::error_code BitcodeReader::MaterializeModule(Module *M) {
33583358
for (Module::iterator F = TheModule->begin(), E = TheModule->end();
33593359
F != E; ++F) {
33603360
if (F->isMaterializable()) {
3361-
if (std::error_code EC = Materialize(F))
3361+
if (std::error_code EC = materialize(F))
33623362
return EC;
33633363
}
33643364
}

llvm/lib/Bitcode/Reader/BitcodeReader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class BitcodeReader : public GVMaterializer {
224224
void releaseBuffer();
225225

226226
bool isDematerializable(const GlobalValue *GV) const override;
227-
std::error_code Materialize(GlobalValue *GV) override;
227+
std::error_code materialize(GlobalValue *GV) override;
228228
std::error_code MaterializeModule(Module *M) override;
229229
void Dematerialize(GlobalValue *GV) override;
230230

llvm/lib/IR/Globals.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ bool GlobalValue::isMaterializable() const {
3636
bool GlobalValue::isDematerializable() const {
3737
return getParent() && getParent()->isDematerializable(this);
3838
}
39-
bool GlobalValue::Materialize(std::string *ErrInfo) {
40-
return getParent()->Materialize(this, ErrInfo);
39+
std::error_code GlobalValue::materialize() {
40+
return getParent()->materialize(this);
4141
}
4242
void GlobalValue::Dematerialize() {
4343
getParent()->Dematerialize(this);

llvm/lib/IR/LegacyPassManager.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,9 +1404,8 @@ void FunctionPassManager::add(Pass *P) {
14041404
///
14051405
bool FunctionPassManager::run(Function &F) {
14061406
if (F.isMaterializable()) {
1407-
std::string errstr;
1408-
if (F.Materialize(&errstr))
1409-
report_fatal_error("Error reading bitcode file: " + Twine(errstr));
1407+
if (std::error_code EC = F.materialize())
1408+
report_fatal_error("Error reading bitcode file: " + EC.message());
14101409
}
14111410
return FPM->run(F);
14121411
}

llvm/lib/IR/Module.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -395,16 +395,11 @@ bool Module::isDematerializable(const GlobalValue *GV) const {
395395
return false;
396396
}
397397

398-
bool Module::Materialize(GlobalValue *GV, std::string *ErrInfo) {
398+
std::error_code Module::materialize(GlobalValue *GV) {
399399
if (!Materializer)
400-
return false;
401-
402-
std::error_code EC = Materializer->Materialize(GV);
403-
if (!EC)
404-
return false;
405-
if (ErrInfo)
406-
*ErrInfo = EC.message();
407-
return true;
400+
return std::error_code();
401+
402+
return Materializer->materialize(GV);
408403
}
409404

410405
void Module::Dematerialize(GlobalValue *GV) {

llvm/lib/Linker/LinkModules.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,8 +1626,10 @@ bool ModuleLinker::run() {
16261626

16271627
// Materialize if needed.
16281628
if (SF->isMaterializable()) {
1629-
if (SF->Materialize(&ErrorMsg))
1629+
if (std::error_code EC = SF->materialize()) {
1630+
ErrorMsg = EC.message();
16301631
return true;
1632+
}
16311633
}
16321634

16331635
// Skip if no body (function is external).
@@ -1677,8 +1679,10 @@ bool ModuleLinker::run() {
16771679

16781680
// Materialize if needed.
16791681
if (SF->isMaterializable()) {
1680-
if (SF->Materialize(&ErrorMsg))
1682+
if (std::error_code EC = SF->materialize()) {
1683+
ErrorMsg = EC.message();
16811684
return true;
1685+
}
16821686
}
16831687

16841688
// Skip if no body (function is external).

llvm/tools/gold/gold-plugin.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,11 @@ static GlobalObject *makeInternalReplacement(GlobalObject *GO) {
482482
Module *M = GO->getParent();
483483
GlobalObject *Ret;
484484
if (auto *F = dyn_cast<Function>(GO)) {
485-
if (F->isMaterializable())
486-
F->Materialize();
485+
if (F->isMaterializable()) {
486+
if (std::error_code EC = F->materialize())
487+
message(LDPL_FATAL, "LLVM gold plugin has failed to read a function");
488+
489+
}
487490

488491
auto *NewF = Function::Create(F->getFunctionType(), F->getLinkage(),
489492
F->getName(), M);

llvm/tools/llvm-extract/llvm-extract.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,9 @@ int main(int argc, char **argv) {
217217
for (size_t i = 0, e = GVs.size(); i != e; ++i) {
218218
GlobalValue *GV = GVs[i];
219219
if (GV->isMaterializable()) {
220-
std::string ErrInfo;
221-
if (GV->Materialize(&ErrInfo)) {
222-
errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
220+
if (std::error_code EC = GV->materialize()) {
221+
errs() << argv[0] << ": error reading input: " << EC.message()
222+
<< "\n";
223223
return 1;
224224
}
225225
}
@@ -229,18 +229,18 @@ int main(int argc, char **argv) {
229229
SmallPtrSet<GlobalValue *, 8> GVSet(GVs.begin(), GVs.end());
230230
for (auto &G : M->globals()) {
231231
if (!GVSet.count(&G) && G.isMaterializable()) {
232-
std::string ErrInfo;
233-
if (G.Materialize(&ErrInfo)) {
234-
errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
232+
if (std::error_code EC = G.materialize()) {
233+
errs() << argv[0] << ": error reading input: " << EC.message()
234+
<< "\n";
235235
return 1;
236236
}
237237
}
238238
}
239239
for (auto &F : *M) {
240240
if (!GVSet.count(&F) && F.isMaterializable()) {
241-
std::string ErrInfo;
242-
if (F.Materialize(&ErrInfo)) {
243-
errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
241+
if (std::error_code EC = F.materialize()) {
242+
errs() << argv[0] << ": error reading input: " << EC.message()
243+
<< "\n";
244244
return 1;
245245
}
246246
}

llvm/unittests/Bitcode/BitReaderTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ TEST(BitReaderTest, DematerializeFunctionPreservesLinkageType) {
6969

7070
EXPECT_FALSE(verifyModule(*M, &dbgs()));
7171

72-
M->getFunction("func")->Materialize();
72+
M->getFunction("func")->materialize();
7373
EXPECT_FALSE(M->getFunction("func")->empty());
7474
EXPECT_TRUE(M->getFunction("func")->getLinkage() ==
7575
GlobalValue::InternalLinkage);
@@ -121,7 +121,7 @@ TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionBefore) {
121121
EXPECT_FALSE(verifyModule(*M, &dbgs()));
122122

123123
// Materialize @before, pulling in @func.
124-
EXPECT_FALSE(M->getFunction("before")->Materialize());
124+
EXPECT_FALSE(M->getFunction("before")->materialize());
125125
EXPECT_FALSE(M->getFunction("func")->empty());
126126
EXPECT_TRUE(M->getFunction("other")->empty());
127127
EXPECT_FALSE(verifyModule(*M, &dbgs()));
@@ -153,7 +153,7 @@ TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionAfter) {
153153
EXPECT_FALSE(verifyModule(*M, &dbgs()));
154154

155155
// Materialize @after, pulling in @func.
156-
EXPECT_FALSE(M->getFunction("after")->Materialize());
156+
EXPECT_FALSE(M->getFunction("after")->materialize());
157157
EXPECT_FALSE(M->getFunction("func")->empty());
158158
EXPECT_TRUE(M->getFunction("other")->empty());
159159
EXPECT_FALSE(verifyModule(*M, &dbgs()));

0 commit comments

Comments
 (0)