Skip to content

Commit df3478e

Browse files
authored
[LLVM] Add new attribute optdebug to optimize for debugging (#66632)
This patch adds a new fn attribute, `optdebug`, that specifies that optimizations should make decisions that prioritize debug info quality, potentially at the cost of runtime performance. This patch does not add any functional changes triggered by this attribute, only the attribute itself. A subsequent patch will use this flag to disable the post-RA scheduler.
1 parent d0e8f33 commit df3478e

File tree

12 files changed

+42
-4
lines changed

12 files changed

+42
-4
lines changed

llvm/docs/BitCodeFormat.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,7 @@ The integer codes are mapped to well-known attributes as follows.
10931093
* code 85: ``skipprofile``
10941094
* code 86: ``memory``
10951095
* code 87: ``nofpclass``
1096+
* code 88: ``optdebug``
10961097

10971098
.. note::
10981099
The ``allocsize`` attribute has a special encoding for its arguments. Its two

llvm/docs/LangRef.rst

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,7 +1872,8 @@ example:
18721872
passes make choices that keep the code size of this function as small
18731873
as possible and perform optimizations that may sacrifice runtime
18741874
performance in order to minimize the size of the generated code.
1875-
This attribute is incompatible with the ``optnone`` attribute.
1875+
This attribute is incompatible with the ``optdebug`` and ``optnone``
1876+
attributes.
18761877
``naked``
18771878
This attribute disables prologue / epilogue emission for the
18781879
function. This can have very system-specific consequences.
@@ -2025,6 +2026,12 @@ example:
20252026
Note: Comparing address of a global variable to ``null`` may still
20262027
evaluate to false because of a limitation in querying this attribute inside
20272028
constant expressions.
2029+
``optdebug``
2030+
This attribute suggests that optimization passes and code generator passes
2031+
should make choices that try to preserve debug info without significantly
2032+
degrading runtime performance.
2033+
This attribute is incompatible with the ``minsize``, ``optsize``, and
2034+
``optnone`` attributes.
20282035
``optforfuzzing``
20292036
This attribute indicates that this function should be optimized
20302037
for maximum fuzzing signal.
@@ -2034,7 +2041,7 @@ example:
20342041
Code generation defaults to the "fast" instruction selector.
20352042
This attribute cannot be used together with the ``alwaysinline``
20362043
attribute; this attribute is also incompatible
2037-
with the ``minsize`` attribute and the ``optsize`` attribute.
2044+
with the ``minsize``, ``optsize``, and ``optdebug`` attributes.
20382045

20392046
This attribute requires the ``noinline`` attribute to be specified on
20402047
the function as well, so the function is never inlined into any caller.
@@ -2045,7 +2052,8 @@ example:
20452052
passes make choices that keep the code size of this function low,
20462053
and otherwise do optimizations specifically to reduce code size as
20472054
long as they do not significantly impact runtime performance.
2048-
This attribute is incompatible with the ``optnone`` attribute.
2055+
This attribute is incompatible with the ``optdebug`` and ``optnone``
2056+
attributes.
20492057
``"patchable-function"``
20502058
This attribute tells the code generator that the code
20512059
generated for this function needs to follow certain conventions that

llvm/include/llvm/Bitcode/LLVMBitCodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ enum AttributeKindCodes {
713713
ATTR_KIND_SKIP_PROFILE = 85,
714714
ATTR_KIND_MEMORY = 86,
715715
ATTR_KIND_NOFPCLASS = 87,
716+
ATTR_KIND_OPTIMIZE_FOR_DEBUGGING = 88,
716717
};
717718

718719
enum ComdatSelectionKindCodes {

llvm/include/llvm/IR/Attributes.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ def NoSanitizeCoverage : EnumAttr<"nosanitize_coverage", [FnAttr]>;
200200
/// Null pointer in address space zero is valid.
201201
def NullPointerIsValid : EnumAttr<"null_pointer_is_valid", [FnAttr]>;
202202

203+
/// Select optimizations that give decent debug info.
204+
def OptimizeForDebugging : EnumAttr<"optdebug", [FnAttr]>;
205+
203206
/// Select optimizations for best fuzzing signal.
204207
def OptForFuzzing : EnumAttr<"optforfuzzing", [FnAttr]>;
205208

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,6 +1980,8 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
19801980
return Attribute::NoSanitizeCoverage;
19811981
case bitc::ATTR_KIND_NULL_POINTER_IS_VALID:
19821982
return Attribute::NullPointerIsValid;
1983+
case bitc::ATTR_KIND_OPTIMIZE_FOR_DEBUGGING:
1984+
return Attribute::OptimizeForDebugging;
19831985
case bitc::ATTR_KIND_OPT_FOR_FUZZING:
19841986
return Attribute::OptForFuzzing;
19851987
case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,8 @@ static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
747747
return bitc::ATTR_KIND_NO_SANITIZE_COVERAGE;
748748
case Attribute::NullPointerIsValid:
749749
return bitc::ATTR_KIND_NULL_POINTER_IS_VALID;
750+
case Attribute::OptimizeForDebugging:
751+
return bitc::ATTR_KIND_OPTIMIZE_FOR_DEBUGGING;
750752
case Attribute::OptForFuzzing:
751753
return bitc::ATTR_KIND_OPT_FOR_FUZZING;
752754
case Attribute::OptimizeForSize:

llvm/lib/IR/Verifier.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,6 +2118,17 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
21182118

21192119
Check(!Attrs.hasFnAttr(Attribute::MinSize),
21202120
"Attributes 'minsize and optnone' are incompatible!", V);
2121+
2122+
Check(!Attrs.hasFnAttr(Attribute::OptimizeForDebugging),
2123+
"Attributes 'optdebug and optnone' are incompatible!", V);
2124+
}
2125+
2126+
if (Attrs.hasFnAttr(Attribute::OptimizeForDebugging)) {
2127+
Check(!Attrs.hasFnAttr(Attribute::OptimizeForSize),
2128+
"Attributes 'optsize and optdebug' are incompatible!", V);
2129+
2130+
Check(!Attrs.hasFnAttr(Attribute::MinSize),
2131+
"Attributes 'minsize and optdebug' are incompatible!", V);
21212132
}
21222133

21232134
if (Attrs.hasFnAttr("aarch64_pstate_sm_enabled")) {

llvm/lib/Transforms/Utils/CodeExtractor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,7 @@ Function *CodeExtractor::constructFunction(const ValueSet &inputs,
941941
case Attribute::NoSanitizeBounds:
942942
case Attribute::NoSanitizeCoverage:
943943
case Attribute::NullPointerIsValid:
944+
case Attribute::OptimizeForDebugging:
944945
case Attribute::OptForFuzzing:
945946
case Attribute::OptimizeNone:
946947
case Attribute::OptimizeForSize:

llvm/test/Bitcode/attributes.ll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,12 @@ define void @f87() fn_ret_thunk_extern { ret void }
511511
; CHECK: define void @f88() [[SKIPPROFILE:#[0-9]+]]
512512
define void @f88() skipprofile { ret void }
513513

514+
define void @f89() optdebug
515+
; CHECK: define void @f89() [[OPTDEBUG:#[0-9]+]]
516+
{
517+
ret void;
518+
}
519+
514520
; CHECK: attributes #0 = { noreturn }
515521
; CHECK: attributes #1 = { nounwind }
516522
; CHECK: attributes #2 = { memory(none) }
@@ -566,4 +572,5 @@ define void @f88() skipprofile { ret void }
566572
; CHECK: attributes #52 = { nosanitize_bounds }
567573
; CHECK: attributes [[FNRETTHUNKEXTERN]] = { fn_ret_thunk_extern }
568574
; CHECK: attributes [[SKIPPROFILE]] = { skipprofile }
575+
; CHECK: attributes [[OPTDEBUG]] = { optdebug }
569576
; CHECK: attributes #[[NOBUILTIN]] = { nobuiltin }

llvm/utils/emacs/llvm-mode.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
'("alwaysinline" "argmemonly" "allocsize" "builtin" "cold" "convergent" "dereferenceable" "dereferenceable_or_null" "hot" "immarg" "inaccessiblememonly"
2626
"inaccessiblemem_or_argmemonly" "inalloca" "inlinehint" "jumptable" "minsize" "mustprogress" "naked" "nobuiltin" "nonnull" "nocapture"
2727
"nocallback" "nocf_check" "noduplicate" "nofree" "noimplicitfloat" "noinline" "nomerge" "nonlazybind" "noprofile" "noredzone" "noreturn"
28-
"norecurse" "nosync" "noundef" "nounwind" "nosanitize_bounds" "nosanitize_coverage" "null_pointer_is_valid" "optforfuzzing" "optnone" "optsize" "preallocated" "readnone" "readonly" "returned" "returns_twice"
28+
"norecurse" "nosync" "noundef" "nounwind" "nosanitize_bounds" "nosanitize_coverage" "null_pointer_is_valid" "optdebug" "optforfuzzing" "optnone" "optsize" "preallocated" "readnone" "readonly" "returned" "returns_twice"
2929
"shadowcallstack" "signext" "speculatable" "speculative_load_hardening" "ssp" "sspreq" "sspstrong" "safestack" "sanitize_address" "sanitize_hwaddress" "sanitize_memtag"
3030
"sanitize_thread" "sanitize_memory" "strictfp" "swifterror" "uwtable" "vscale_range" "willreturn" "writeonly" "zeroext") 'symbols) . font-lock-constant-face)
3131
;; Variables

0 commit comments

Comments
 (0)