aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/CIR/CodeGen/CIRGenModule.cpp
diff options
context:
space:
mode:
author7mile <i@7li.moe>2025-08-22 00:51:37 +0800
committerGitHub <noreply@github.com>2025-08-21 09:51:37 -0700
commit761125f2676925fbc4ef23f8b5cc937ebea9a0fb (patch)
tree3e8824a8884422f710af6f0a1581287f315d27d2 /clang/lib/CIR/CodeGen/CIRGenModule.cpp
parent436f391d1d560822a04d11877c20783e51c1c365 (diff)
downloadllvm-761125f2676925fbc4ef23f8b5cc937ebea9a0fb.zip
llvm-761125f2676925fbc4ef23f8b5cc937ebea9a0fb.tar.gz
llvm-761125f2676925fbc4ef23f8b5cc937ebea9a0fb.tar.bz2
[CIR][Dialect] Add SourceLangAttr (#152511)
This patch upstreams `SourceLangAttr` and its CodeGen logic in the CGM, which encodes the source language in CIR.
Diffstat (limited to 'clang/lib/CIR/CodeGen/CIRGenModule.cpp')
-rw-r--r--clang/lib/CIR/CodeGen/CIRGenModule.cpp20
1 files changed, 20 insertions, 0 deletions
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index cb8cc30..a557d2a 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -103,6 +103,9 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
PtrDiffTy =
cir::IntType::get(&getMLIRContext(), sizeTypeSize, /*isSigned=*/true);
+ theModule->setAttr(
+ cir::CIRDialect::getSourceLanguageAttrName(),
+ cir::SourceLanguageAttr::get(&mlirContext, getCIRSourceLanguage()));
theModule->setAttr(cir::CIRDialect::getTripleAttrName(),
builder.getStringAttr(getTriple().str()));
@@ -510,6 +513,23 @@ void CIRGenModule::setNonAliasAttributes(GlobalDecl gd, mlir::Operation *op) {
assert(!cir::MissingFeatures::setTargetAttributes());
}
+cir::SourceLanguage CIRGenModule::getCIRSourceLanguage() const {
+ using ClangStd = clang::LangStandard;
+ using CIRLang = cir::SourceLanguage;
+ auto opts = getLangOpts();
+
+ if (opts.CPlusPlus)
+ return CIRLang::CXX;
+ if (opts.C99 || opts.C11 || opts.C17 || opts.C23 || opts.C2y ||
+ opts.LangStd == ClangStd::lang_c89 ||
+ opts.LangStd == ClangStd::lang_gnu89)
+ return CIRLang::C;
+
+ // TODO(cir): support remaining source languages.
+ assert(!cir::MissingFeatures::sourceLanguageCases());
+ errorNYI("CIR does not yet support the given source language");
+}
+
static void setLinkageForGV(cir::GlobalOp &gv, const NamedDecl *nd) {
// Set linkage and visibility in case we never see a definition.
LinkageInfo lv = nd->getLinkageAndVisibility();