blob: 6020abc463edaa1cc14fabf3332bb90c19528ad7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
//===- unittests/Frontend/CodeGenActionTest.cpp --- FrontendAction tests --===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Unit tests for CodeGenAction.
//
//===----------------------------------------------------------------------===//
#include "mlir/IR/Builders.h"
#include "flang/Frontend/CompilerInstance.h"
#include "flang/Frontend/FrontendActions.h"
#include "flang/Frontend/TextDiagnosticPrinter.h"
#include "gtest/gtest.h"
#include <memory>
using namespace Fortran::frontend;
namespace test {
class DummyDialect : public ::mlir::Dialect {
explicit DummyDialect(::mlir::MLIRContext *context)
: ::mlir::Dialect(getDialectNamespace(), context,
::mlir::TypeID::get<DummyDialect>()) {
initialize();
}
void initialize();
friend class ::mlir::MLIRContext;
public:
~DummyDialect() override = default;
static constexpr ::llvm::StringLiteral getDialectNamespace() {
return ::llvm::StringLiteral("dummy");
}
};
namespace dummy {
class FakeOp : public ::mlir::Op<FakeOp> {
public:
using Op::Op;
static llvm::StringRef getOperationName() { return "dummy.fake"; }
static ::llvm::ArrayRef<::llvm::StringRef> getAttributeNames() { return {}; }
static void build(
::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState) {}
};
} // namespace dummy
} // namespace test
MLIR_DECLARE_EXPLICIT_TYPE_ID(::test::DummyDialect)
MLIR_DEFINE_EXPLICIT_TYPE_ID(::test::DummyDialect)
namespace test {
void DummyDialect::initialize() { addOperations<::test::dummy::FakeOp>(); }
} // namespace test
// A test CodeGenAction to verify that we gracefully handle failure to convert
// from MLIR to LLVM IR.
class LLVMConversionFailureCodeGenAction : public CodeGenAction {
public:
LLVMConversionFailureCodeGenAction()
: CodeGenAction(BackendActionTy::Backend_EmitLL) {
mlirCtx = std::make_unique<mlir::MLIRContext>();
mlirCtx->loadDialect<test::DummyDialect>();
mlir::Location loc(mlir::UnknownLoc::get(mlirCtx.get()));
mlirModule = mlir::ModuleOp::create(loc, "mod");
mlir::OpBuilder builder(mlirCtx.get());
builder.setInsertionPointToStart(&mlirModule->getRegion().front());
// Create a fake op to trip conversion to LLVM.
builder.create<test::dummy::FakeOp>(loc);
llvmCtx = std::make_unique<llvm::LLVMContext>();
}
};
TEST(CodeGenAction, GracefullyHandleLLVMConversionFailure) {
std::string diagnosticOutput;
llvm::raw_string_ostream diagnosticsOS(diagnosticOutput);
clang::DiagnosticOptions diagOpts;
auto diagPrinter = std::make_unique<Fortran::frontend::TextDiagnosticPrinter>(
diagnosticsOS, diagOpts);
CompilerInstance ci;
ci.createDiagnostics(diagPrinter.get(), /*ShouldOwnClient=*/false);
ci.setInvocation(std::make_shared<CompilerInvocation>());
ci.setOutputStream(std::make_unique<llvm::raw_null_ostream>());
ci.getInvocation().getCodeGenOpts().OptimizationLevel = 0;
FrontendInputFile file("/dev/null", InputKind());
LLVMConversionFailureCodeGenAction action;
action.setInstance(&ci);
action.setCurrentInput(file);
consumeError(action.execute());
ASSERT_EQ(diagnosticOutput,
"error: Lowering to LLVM IR failed\n"
"error: failed to create the LLVM module\n");
}
|