Skip to content

[CIR][Dialect] Add SourceLangAttr #152511

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 3 commits 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
39 changes: 39 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,45 @@ class CIR_UnitAttr<string name, string attrMnemonic, list<Trait> traits = []>
let isOptional = 1;
}

//===----------------------------------------------------------------------===//
// SourceLanguageAttr
//===----------------------------------------------------------------------===//

// TODO: Add cases for other languages that Clang supports.

def CIR_SourceLanguage : CIR_I32EnumAttr<"SourceLanguage", "source language", [
I32EnumAttrCase<"C", 1, "c">,
I32EnumAttrCase<"CXX", 2, "cxx">
]> {
// The enum attr class is defined in `CIR_SourceLanguageAttr` below,
// so that it can define extra class methods.
let genSpecializedAttr = 0;
}

def CIR_SourceLanguageAttr : CIR_EnumAttr<CIR_SourceLanguage, "lang"> {

let summary = "Module source language";
let description = [{
Represents the source language used to generate the module.

Example:
```
// Module compiled from C.
module attributes {cir.lang = cir.lang<c>} {}
// Module compiled from C++.
module attributes {cir.lang = cir.lang<cxx>} {}
```

Module source language attribute name is `cir.lang` is defined by
`getSourceLanguageAttrName` method in CIRDialect class.
}];

let extraClassDeclaration = [{
bool isC() const { return getValue() == SourceLanguage::C; }
bool isCXX() const { return getValue() == SourceLanguage::CXX; }
}];
}

//===----------------------------------------------------------------------===//
// OptInfoAttr
//===----------------------------------------------------------------------===//
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRDialect.td
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def CIR_Dialect : Dialect {
let hasConstantMaterializer = 1;

let extraClassDeclaration = [{
static llvm::StringRef getSourceLanguageAttrName() { return "cir.lang"; }
static llvm::StringRef getTripleAttrName() { return "cir.triple"; }
static llvm::StringRef getOptInfoAttrName() { return "cir.opt_info"; }
static llvm::StringRef getCalleeAttrName() { return "callee"; }
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/CIR/MissingFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ struct MissingFeatures {
static bool setNonGC() { return false; }
static bool setObjCGCLValueClass() { return false; }
static bool setTargetAttributes() { return false; }
static bool sourceLanguageCases() { return false; }
static bool stackBase() { return false; }
static bool stackSaveOp() { return false; }
static bool targetCIRGenInfoArch() { return false; }
Expand Down
22 changes: 22 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,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()));

Expand Down Expand Up @@ -495,6 +498,25 @@ 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 || opts.CPlusPlus11 || opts.CPlusPlus14 ||
opts.CPlusPlus17 || opts.CPlusPlus20 || opts.CPlusPlus23 ||
opts.CPlusPlus26)
return CIRLang::CXX;
Copy link
Contributor

Choose a reason for hiding this comment

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

Would we want to also encode the language standard used? That's not necessary for this PR, just something to consider.

if (opts.C99 || opts.C11 || opts.C17 || opts.C23 ||
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();
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,9 @@ class CIRGenModule : public CIRGenTypeCache {
void replacePointerTypeArgs(cir::FuncOp oldF, cir::FuncOp newF);

void setNonAliasAttributes(GlobalDecl gd, mlir::Operation *op);

/// Map source language used to a CIR attribute.
cir::SourceLanguage getCIRSourceLanguage() const;
};
} // namespace CIRGen

Expand Down
8 changes: 8 additions & 0 deletions clang/test/CIR/CodeGen/lang-c.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s

// CIR: module attributes {{{.*}}cir.lang = #cir.lang<c>{{.*}}}

int main() {
return 0;
}
8 changes: 8 additions & 0 deletions clang/test/CIR/CodeGen/lang-cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be combined with the lang-c.c test using the -x c option.

// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s

// CIR: module attributes {{{.*}}cir.lang = #cir.lang<cxx>{{.*}}}

int main() {
return 0;
}
5 changes: 5 additions & 0 deletions clang/test/CIR/IR/invalid-lang-attr.cir
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// RUN: cir-opt %s -verify-diagnostics

// expected-error@below {{expected ::cir::SourceLanguage to be one of}}
// expected-error@below {{failed to parse CIR_SourceLanguageAttr parameter 'value'}}
module attributes {cir.lang = #cir.lang<dummy>} { }
12 changes: 12 additions & 0 deletions clang/test/CIR/IR/module.cir
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: cir-opt %s -split-input-file -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s

// Should parse and print C source language attribute.
module attributes {cir.lang = #cir.lang<c>} { }
// CHECK: module attributes {cir.lang = #cir.lang<c>}

// -----

// Should parse and print C++ source language attribute.
module attributes {cir.lang = #cir.lang<cxx>} { }
// CHECK: module attributes {cir.lang = #cir.lang<cxx>}