Skip to content

Commit

Permalink
[Flang][Unit Test] Move the declaration of kindMap to the class
Browse files Browse the repository at this point in the history
kindMap variable is declared in the Setup function but passed as
a reference to the firBuilder class. The firBuilder is declared in
the class and hence its lifetime exceeds that of kindMap. This can
lead to undefined behaviour. Move the kindMap variable into the class
to avoid this.

This is part of the upstreaming effort from the fir-dev branch in [1].
[1] https://github.com/flang-compiler/f18-llvm-project

Reviewed By: awarzynski

Differential Revision: https://reviews.llvm.org/D115631
  • Loading branch information
kiranchandramohan committed Dec 13, 2021
1 parent b8c12af commit f97731c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions flang/unittests/Optimizer/Builder/CharacterTest.cpp
Expand Up @@ -16,7 +16,7 @@
struct CharacterTest : public testing::Test {
public:
void SetUp() override {
fir::KindMapping kindMap(&context,
kindMap = std::make_unique<fir::KindMapping>(&context,
"i10:80,l3:24,a1:8,r54:Double,c20:X86_FP80,r11:PPC_FP128,"
"r12:FP128,r13:X86_FP80,r14:Double,r15:Float,r16:Half,r23:BFloat");
mlir::OpBuilder builder(&context);
Expand All @@ -32,12 +32,13 @@ struct CharacterTest : public testing::Test {
builder.setInsertionPointToStart(entryBlock);

fir::support::loadDialects(context);
firBuilder = std::make_unique<fir::FirOpBuilder>(mod, kindMap);
firBuilder = std::make_unique<fir::FirOpBuilder>(mod, *kindMap);
}

fir::FirOpBuilder &getBuilder() { return *firBuilder; }

mlir::MLIRContext context;
std::unique_ptr<fir::KindMapping> kindMap;
std::unique_ptr<fir::FirOpBuilder> firBuilder;
};

Expand Down
5 changes: 3 additions & 2 deletions flang/unittests/Optimizer/Builder/DoLoopHelperTest.cpp
Expand Up @@ -15,16 +15,17 @@
struct DoLoopHelperTest : public testing::Test {
public:
void SetUp() {
fir::KindMapping kindMap(&context);
kindMap = std::make_unique<fir::KindMapping>(&context);
mlir::OpBuilder builder(&context);
firBuilder = new fir::FirOpBuilder(builder, kindMap);
firBuilder = new fir::FirOpBuilder(builder, *kindMap);
fir::support::loadDialects(context);
}
void TearDown() { delete firBuilder; }

fir::FirOpBuilder &getBuilder() { return *firBuilder; }

mlir::MLIRContext context;
std::unique_ptr<fir::KindMapping> kindMap;
fir::FirOpBuilder *firBuilder;
};

Expand Down

0 comments on commit f97731c

Please sign in to comment.