Skip to content

Commit 378e9bb

Browse files
authored
[cir-translate] Fix crash issue where the data layout string is missing (#147209)
- Targets like 'aarch64' or 'arm' only populate the data layout string after the constructor. Need to call 'CreateTargetInfo' to setup them properly.
1 parent c4f18d6 commit 378e9bb

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

clang/test/CIR/Lowering/select.cir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: cir-translate -cir-to-llvmir --disable-cc-lowering -o %t.ll %s
2-
// RUN: FileCheck --input-file=%t.ll -check-prefix=LLVM %s
1+
// RUN: cir-translate -cir-to-llvmir --disable-cc-lowering -o - %s | FileCheck -check-prefix=LLVM %s
2+
// RUN: cir-translate -target aarch64 -cir-to-llvmir --disable-cc-lowering -o - %s | FileCheck -check-prefix=LLVM %s
33

44
!s32i = !cir.int<s, 32>
55

clang/tools/cir-translate/cir-translate.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#include "llvm/IR/Module.h"
2626
#include "llvm/TargetParser/Host.h"
2727

28+
#include "clang/Basic/Diagnostic.h"
29+
#include "clang/Basic/DiagnosticIDs.h"
30+
#include "clang/Basic/DiagnosticOptions.h"
2831
#include "clang/Basic/TargetInfo.h"
2932
#include "clang/CIR/Dialect/IR/CIRDialect.h"
3033
#include "clang/CIR/Dialect/Passes.h"
@@ -82,12 +85,19 @@ llvm::LogicalResult prepareCIRModuleDataLayout(mlir::ModuleOp mod,
8285

8386
// Data layout is fully determined by the target triple. Here we only pass the
8487
// triple to get the data layout.
88+
llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagID(
89+
new clang::DiagnosticIDs);
90+
clang::DiagnosticOptions diagOpts;
91+
llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics =
92+
new clang::DiagnosticsEngine(diagID, diagOpts,
93+
new clang::IgnoringDiagConsumer());
8594
llvm::Triple triple(rawTriple);
95+
// TODO: Need to set various target options later to populate
96+
// 'TargetInfo' properly.
8697
clang::TargetOptions targetOptions;
8798
targetOptions.Triple = rawTriple;
88-
// FIXME: AllocateTarget is a big deal. Better make it a global state.
89-
std::unique_ptr<clang::TargetInfo> targetInfo =
90-
clang::targets::AllocateTarget(llvm::Triple(rawTriple), targetOptions);
99+
llvm::IntrusiveRefCntPtr<clang::TargetInfo> targetInfo =
100+
clang::TargetInfo::CreateTargetInfo(*diagnostics, targetOptions);
91101
if (!targetInfo) {
92102
mod.emitError() << "error: invalid target triple '" << rawTriple << "'\n";
93103
return llvm::failure();

0 commit comments

Comments
 (0)