Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 37 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,43 @@ class CIR_UnitAttr<string name, string attrMnemonic, list<Trait> traits = []>
let isOptional = 1;
}

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

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
24 changes: 24 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,27 @@ 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.OpenCL && !opts.OpenCLCPlusPlus)
llvm_unreachable("NYI");

if (opts.CPlusPlus || opts.CPlusPlus11 || opts.CPlusPlus14 ||
opts.CPlusPlus17 || opts.CPlusPlus20 || opts.CPlusPlus23 ||
opts.CPlusPlus26)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would hope opts.CPlusPlus implies arbitrary standard. The additional checks are redundant. This could be simplified to just?

Suggested change
if (opts.CPlusPlus || opts.CPlusPlus11 || opts.CPlusPlus14 ||
opts.CPlusPlus17 || opts.CPlusPlus20 || opts.CPlusPlus23 ||
opts.CPlusPlus26)
if (opts.CPlusPlus)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Make sense! Updated.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, I'll give it a try in a later patch.

if (opts.C99 || opts.C11 || opts.C17 || opts.C23 ||
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (opts.C99 || opts.C11 || opts.C17 || opts.C23 ||
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.
llvm_unreachable("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>}
Loading