[llvm] [Coroutines] Create C++ noop coroutine with default function attributes (PR #134878)

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 8 09:32:22 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Victor Campos (vhscampos)

<details>
<summary>Changes</summary>

This change makes the C++ noop coroutine to be built with attributes based on the module flags. Among other things, this adds function attributes related to the Pointer Authentication and Branch Target Enforcement module flags, which are set by command-line options.

Before this patch, C++ programs built with either PAC-RET or BTI that had the noop coroutine emitted could have a runtime error because this function wasn't compatible with the rest of the program.

---
Full diff: https://github.com/llvm/llvm-project/pull/134878.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/Coroutines/CoroEarly.cpp (+4-3) 
- (added) llvm/test/Transforms/Coroutines/coro-noop-pacbti.ll (+23) 


``````````diff
diff --git a/llvm/lib/Transforms/Coroutines/CoroEarly.cpp b/llvm/lib/Transforms/Coroutines/CoroEarly.cpp
index 5375448d2d2e2..fd0c5e2e240c2 100644
--- a/llvm/lib/Transforms/Coroutines/CoroEarly.cpp
+++ b/llvm/lib/Transforms/Coroutines/CoroEarly.cpp
@@ -130,9 +130,10 @@ void Lowerer::lowerCoroNoop(IntrinsicInst *II) {
         StructType::create({FnPtrTy, FnPtrTy}, "NoopCoro.Frame");
 
     // Create a Noop function that does nothing.
-    Function *NoopFn =
-        Function::Create(FnTy, GlobalValue::LinkageTypes::PrivateLinkage,
-                         "__NoopCoro_ResumeDestroy", &M);
+    Function *NoopFn = Function::createWithDefaultAttr(
+        FnTy, GlobalValue::LinkageTypes::PrivateLinkage,
+        M.getDataLayout().getProgramAddressSpace(), "__NoopCoro_ResumeDestroy",
+        &M);
     NoopFn->setCallingConv(CallingConv::Fast);
     buildDebugInfoForNoopResumeDestroyFunc(NoopFn);
     auto *Entry = BasicBlock::Create(C, "entry", NoopFn);
diff --git a/llvm/test/Transforms/Coroutines/coro-noop-pacbti.ll b/llvm/test/Transforms/Coroutines/coro-noop-pacbti.ll
new file mode 100644
index 0000000000000..4f302a6acc649
--- /dev/null
+++ b/llvm/test/Transforms/Coroutines/coro-noop-pacbti.ll
@@ -0,0 +1,23 @@
+
+; RUN: opt < %s -S -passes=coro-early | FileCheck %s
+
+; CHECK:      define private fastcc void @__NoopCoro_ResumeDestroy(ptr %0) #1 {
+; CHECK-NEXT: entry:
+; CHECK-NEXT:   ret void
+; CHECK-NEXT: }
+
+; CHECK:      attributes #1 = { "branch-target-enforcement" "sign-return-address"="all" "sign-return-address-key"="a_key" }
+
+define ptr @noop() {
+entry:
+  %n = call ptr @llvm.coro.noop()
+  ret ptr %n
+}
+
+declare ptr @llvm.coro.noop()
+
+!llvm.module.flags = !{!0, !1, !2}
+
+!0 = !{i32 8, !"branch-target-enforcement", i32 1}
+!1 = !{i32 8, !"sign-return-address", i32 1}
+!2 = !{i32 8, !"sign-return-address-all", i32 1}

``````````

</details>


https://github.com/llvm/llvm-project/pull/134878


More information about the llvm-commits mailing list