Skip to content

Commit 2e8c10e

Browse files
committed
Add flatten_deep attribute in LLVM
1 parent 50ce2d7 commit 2e8c10e

File tree

12 files changed

+118
-0
lines changed

12 files changed

+118
-0
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2751,6 +2751,15 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
27512751
B.addAttribute("aarch64_new_zt0");
27522752
}
27532753

2754+
// Handle flatten_deep attribute for depth-based inlining
2755+
if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2756+
if (const FlattenDeepAttr *FDA = FD->getAttr<FlattenDeepAttr>()) {
2757+
// Add the flatten_deep attribute with the max depth value as a typed int
2758+
// attribute
2759+
B.addRawIntAttr(llvm::Attribute::FlattenDeep, FDA->getMaxDepth());
2760+
}
2761+
}
2762+
27542763
// Track whether we need to add the optnone LLVM attribute,
27552764
// starting with the default for this optimization level.
27562765
bool ShouldAddOptNone =
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %clang_cc1 -emit-llvm -o - %s | FileCheck %s
2+
3+
// CHECK-LABEL: define {{.*}} @test1
4+
// CHECK-SAME: #[[ATTR1:[0-9]+]]
5+
__attribute__((flatten_deep(3)))
6+
void test1() {
7+
}
8+
9+
// Verify the attribute is present in the attribute groups
10+
// CHECK-DAG: attributes #[[ATTR1]] = { {{.*}}flatten_deep=3{{.*}} }

llvm/include/llvm/AsmParser/LLParser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ namespace llvm {
320320
bool AllowParens = false);
321321
bool parseOptionalCodeModel(CodeModel::Model &model);
322322
bool parseOptionalDerefAttrBytes(lltok::Kind AttrKind, uint64_t &Bytes);
323+
bool parseOptionalFlattenDeepDepth(lltok::Kind AttrKind, uint64_t &Depth);
323324
bool parseOptionalUWTableKind(UWTableKind &Kind);
324325
bool parseAllocKind(AllocFnKind &Kind);
325326
std::optional<MemoryEffects> parseMemoryAttr();

llvm/include/llvm/Bitcode/LLVMBitCodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,7 @@ enum AttributeKindCodes {
802802
ATTR_KIND_DEAD_ON_RETURN = 103,
803803
ATTR_KIND_SANITIZE_ALLOC_TOKEN = 104,
804804
ATTR_KIND_NO_CREATE_UNDEF_OR_POISON = 105,
805+
ATTR_KIND_FLATTEN_DEEP = 106,
805806
};
806807

807808
enum ComdatSelectionKindCodes {

llvm/include/llvm/IR/Attributes.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,10 @@ class AttrBuilder {
12461246
/// form used internally in Attribute.
12471247
LLVM_ABI AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
12481248

1249+
/// This turns the flatten_deep depth into the form used internally in
1250+
/// Attribute.
1251+
LLVM_ABI AttrBuilder &addFlattenDeepAttr(uint64_t Depth);
1252+
12491253
/// This turns one (or two) ints into the form used internally in Attribute.
12501254
LLVM_ABI AttrBuilder &
12511255
addAllocSizeAttr(unsigned ElemSizeArg,

llvm/include/llvm/IR/Attributes.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ def NoImplicitFloat : EnumAttr<"noimplicitfloat", IntersectPreserve, [FnAttr]>;
212212
/// inline=never.
213213
def NoInline : EnumAttr<"noinline", IntersectPreserve, [FnAttr]>;
214214

215+
/// Inline calls recursively up to specified depth.
216+
def FlattenDeep : IntAttr<"flatten_deep", IntersectPreserve, [FnAttr]>;
217+
215218
/// Function is called early and/or often, so lazy binding isn't worthwhile.
216219
def NonLazyBind : EnumAttr<"nonlazybind", IntersectPreserve, [FnAttr]>;
217220

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,23 @@ bool LLParser::parseEnumAttribute(Attribute::AttrKind Attr, AttrBuilder &B,
16201620
B.addDereferenceableOrNullAttr(Bytes);
16211621
return false;
16221622
}
1623+
case Attribute::FlattenDeep: {
1624+
uint64_t Depth = 0;
1625+
if (InAttrGroup) {
1626+
Lex.Lex();
1627+
LocTy DepthLoc = Lex.getLoc();
1628+
if (parseToken(lltok::equal, "expected '=' here") ||
1629+
parseUInt64(Depth))
1630+
return true;
1631+
if (!Depth)
1632+
return error(DepthLoc, "flatten_deep depth must be non-zero");
1633+
} else {
1634+
if (parseOptionalFlattenDeepDepth(lltok::kw_flatten_deep, Depth))
1635+
return true;
1636+
}
1637+
B.addFlattenDeepAttr(Depth);
1638+
return false;
1639+
}
16231640
case Attribute::UWTable: {
16241641
UWTableKind Kind;
16251642
if (parseOptionalUWTableKind(Kind))
@@ -2494,6 +2511,30 @@ bool LLParser::parseOptionalDerefAttrBytes(lltok::Kind AttrKind,
24942511
return false;
24952512
}
24962513

2514+
/// parseOptionalFlattenDeepDepth
2515+
/// ::= /* empty */
2516+
/// ::= 'flatten_deep' '(' 4 ')'
2517+
bool LLParser::parseOptionalFlattenDeepDepth(lltok::Kind AttrKind,
2518+
uint64_t &Depth) {
2519+
assert(AttrKind == lltok::kw_flatten_deep && "contract!");
2520+
2521+
Depth = 0;
2522+
if (!EatIfPresent(AttrKind))
2523+
return false;
2524+
LocTy ParenLoc = Lex.getLoc();
2525+
if (!EatIfPresent(lltok::lparen))
2526+
return error(ParenLoc, "expected '('");
2527+
LocTy DepthLoc = Lex.getLoc();
2528+
if (parseUInt64(Depth))
2529+
return true;
2530+
ParenLoc = Lex.getLoc();
2531+
if (!EatIfPresent(lltok::rparen))
2532+
return error(ParenLoc, "expected ')'");
2533+
if (!Depth)
2534+
return error(DepthLoc, "flatten_deep depth must be non-zero");
2535+
return false;
2536+
}
2537+
24972538
bool LLParser::parseOptionalUWTableKind(UWTableKind &Kind) {
24982539
Lex.Lex();
24992540
Kind = UWTableKind::Default;

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,6 +2077,8 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
20772077
return Attribute::DisableSanitizerInstrumentation;
20782078
case bitc::ATTR_KIND_ELEMENTTYPE:
20792079
return Attribute::ElementType;
2080+
case bitc::ATTR_KIND_FLATTEN_DEEP:
2081+
return Attribute::FlattenDeep;
20802082
case bitc::ATTR_KIND_FNRETTHUNK_EXTERN:
20812083
return Attribute::FnRetThunkExtern;
20822084
case bitc::ATTR_KIND_INLINE_HINT:
@@ -2393,6 +2395,8 @@ Error BitcodeReader::parseAttributeGroupBlock() {
23932395
B.addDereferenceableAttr(Record[++i]);
23942396
else if (Kind == Attribute::DereferenceableOrNull)
23952397
B.addDereferenceableOrNullAttr(Record[++i]);
2398+
else if (Kind == Attribute::FlattenDeep)
2399+
B.addFlattenDeepAttr(Record[++i]);
23962400
else if (Kind == Attribute::AllocSize)
23972401
B.addAllocSizeAttrFromRawRepr(Record[++i]);
23982402
else if (Kind == Attribute::VScaleRange)

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,8 @@ static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
958958
return bitc::ATTR_KIND_DEAD_ON_RETURN;
959959
case Attribute::NoCreateUndefOrPoison:
960960
return bitc::ATTR_KIND_NO_CREATE_UNDEF_OR_POISON;
961+
case Attribute::FlattenDeep:
962+
return bitc::ATTR_KIND_FLATTEN_DEEP;
961963
case Attribute::EndAttrKinds:
962964
llvm_unreachable("Can not encode end-attribute kinds marker.");
963965
case Attribute::None:

llvm/lib/IR/Attributes.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,9 @@ std::string Attribute::getAsString(bool InAttrGrp) const {
574574
if (hasAttribute(Attribute::DereferenceableOrNull))
575575
return AttrWithBytesToString("dereferenceable_or_null");
576576

577+
if (hasAttribute(Attribute::FlattenDeep))
578+
return AttrWithBytesToString("flatten_deep");
579+
577580
if (hasAttribute(Attribute::AllocSize)) {
578581
unsigned ElemSize;
579582
std::optional<unsigned> NumElems;
@@ -2206,6 +2209,13 @@ AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
22062209
return addRawIntAttr(Attribute::DereferenceableOrNull, Bytes);
22072210
}
22082211

2212+
AttrBuilder &AttrBuilder::addFlattenDeepAttr(uint64_t Depth) {
2213+
if (Depth == 0)
2214+
return *this;
2215+
2216+
return addRawIntAttr(Attribute::FlattenDeep, Depth);
2217+
}
2218+
22092219
AttrBuilder &
22102220
AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
22112221
const std::optional<unsigned> &NumElems) {

0 commit comments

Comments
 (0)