[clang] [clang][DebugInfo] Add inlined subprogram metadata for compiler built-ins (PR #189969)
Dan Liew via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 17 10:03:13 PDT 2026
================
@@ -2642,12 +2642,80 @@ static RValue EmitHipStdParUnsupportedBuiltin(CodeGenFunction *CGF,
return RValue::get(CGF->Builder.CreateCall(UBF, Args));
}
+/// Determines whether we should treat a given built-in as a call to an
+/// artificial inlined function in debug info.
+static bool shouldUseBuiltinDebugLocation(unsigned BuiltinID) {
+ switch (BuiltinID) {
+ case Builtin::BI__builtin_unpredictable:
+ case Builtin::BI__builtin_expect:
+ case Builtin::BI__builtin_expect_with_probability:
+ case Builtin::BI__builtin_assume_aligned:
+ case Builtin::BI__builtin_assume_dereferenceable:
+ case Builtin::BI__assume:
+ case Builtin::BI__builtin_assume:
+ case Builtin::BI__builtin_assume_separate_storage:
+ case Builtin::BI__builtin_allow_runtime_check:
+ case Builtin::BI__builtin_allow_sanitize_check:
+ case Builtin::BI__builtin_constant_p:
+ case Builtin::BI__builtin_prefetch:
+ case Builtin::BI__builtin___clear_cache:
+ case Builtin::BI__builtin_unreachable: {
+ // Exclude optimisation hint built-ins. These are a form of communicating
+ // additional constraints to the compiler.
+ return false;
+ }
+ case Builtin::BI__builtin_trap:
+ case Builtin::BI__builtin_verbose_trap:
+ case Builtin::BI__debugbreak:
+ case Builtin::BI__builtin_eh_return:
+ case Builtin::BI__builtin_unwind_init:
+ case Builtin::BI__exception_code:
+ case Builtin::BI_exception_code:
+ case Builtin::BI__exception_info:
+ case Builtin::BI_exception_info:
+ case Builtin::BI__abnormal_termination:
+ case Builtin::BI_abnormal_termination: {
+ // Exclude trap and exception built-ins. These may use their own debug
+ // location handling, so we avoid making debug changes so they may inspect
+ // the caller as-is without additional debug info layers.
+ return false;
+ }
+ case Builtin::BI__annotation:
+ case Builtin::BI__builtin_annotation: {
+ // Exclude annotation built-ins. These attach debug-time information.
+ return false;
+ }
+ case Builtin::BI__builtin_operator_new:
+ case Builtin::BI__builtin_operator_delete: {
+ // Exclude C++ operator built-ins. These are emitted as normal calls.
+ return false;
+ }
+ }
+
+ // Most generic (non-target-specific) built-ins are call-like, so we default
+ // to enabling this debug info treatment.
+ return true;
+
+ // See also further exclusions of library functions emitted as normal calls
+ // and target-specific built-ins towards the end of `EmitBuiltinExpr` which
+ // overrides the value returned here.
+}
+
RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
const CallExpr *E,
ReturnValueSlot ReturnValue) {
assert(!getContext().BuiltinInfo.isImmediate(BuiltinID) &&
"Should not codegen for consteval builtins");
+ // Treat built-in as call to artificial inlined function in debug info.
+ // This enables e.g. profiling tools to annotate time spent in user-called
+ // built-ins with the built-in function name.
+ // See `useBuiltinDebugLocation` for cases where this treatment is disabled.
+ auto DebugScope =
----------------
delcypher wrote:
There should probably me some kind of flag (even if hidden) that allows this to switched off. User's of clang that are sensitive to the debug info size may want to turn this off. I think it should be on by default though because it improves debugability.
https://github.com/llvm/llvm-project/pull/189969
More information about the cfe-commits
mailing list