Skip to content

[cir-translate] Fix crash issue where the data layout string is missing #147209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clang/test/CIR/Lowering/select.cir
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: cir-translate -cir-to-llvmir --disable-cc-lowering -o %t.ll %s
// RUN: FileCheck --input-file=%t.ll -check-prefix=LLVM %s
// RUN: cir-translate -cir-to-llvmir --disable-cc-lowering -o - %s | FileCheck -check-prefix=LLVM %s
// RUN: cir-translate -target aarch64 -cir-to-llvmir --disable-cc-lowering -o - %s | FileCheck -check-prefix=LLVM %s

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

Expand Down
12 changes: 10 additions & 2 deletions clang/tools/cir-translate/cir-translate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#include "llvm/IR/Module.h"
#include "llvm/TargetParser/Host.h"

#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/DiagnosticIDs.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/Passes.h"
Expand Down Expand Up @@ -82,12 +85,17 @@ llvm::LogicalResult prepareCIRModuleDataLayout(mlir::ModuleOp mod,

// Data layout is fully determined by the target triple. Here we only pass the
// triple to get the data layout.
llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagID(
new clang::DiagnosticIDs);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you allocating a ref-counted DiagnosticIDs but using a local variable for DiagnosticOptions? The existing examples I found in the clang code seem to use ref-counted pointers for both.

Copy link
Contributor Author

@darkbuck darkbuck Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Through examples, only DiagnosticIDs uses smart pointer. I will double-check.

clang::DiagnosticOptions diagOpts;
clang::DiagnosticsEngine diagnostics(diagID, diagOpts,
new clang::IgnoringDiagConsumer());
llvm::Triple triple(rawTriple);
clang::TargetOptions targetOptions;
targetOptions.Triple = rawTriple;
// FIXME: AllocateTarget is a big deal. Better make it a global state.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update this comment to just refer to TargetOptions? It seems that we're only using the data layout string here, but there are many other things in TargetOptions that might be relevant at some point.

std::unique_ptr<clang::TargetInfo> targetInfo =
clang::targets::AllocateTarget(llvm::Triple(rawTriple), targetOptions);
llvm::IntrusiveRefCntPtr<clang::TargetInfo> targetInfo =
clang::TargetInfo::CreateTargetInfo(diagnostics, targetOptions);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping we had a CreateTargetInfo variation that could do the diagnostic boilerplate, but looks like we don't. Any specific reason we need to switch on using IntrusiveRefCntPtr here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could add one? It seems there are multiple other callers that similarly don't use these.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for adding a variant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's copied from other usage of CreateTargetInfo. It could be replaced with unique_ptr.

if (!targetInfo) {
mod.emitError() << "error: invalid target triple '" << rawTriple << "'\n";
return llvm::failure();
Expand Down
Loading