[llvm] [CodeGen][GC] Skip function without GC in `GCLoweringPass` (PR #84421)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 13 05:54:35 PDT 2024
https://github.com/paperchalice updated https://github.com/llvm/llvm-project/pull/84421
>From e1668c840819c5ff2ebe28424bcd54a9a2abc484 Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Fri, 8 Mar 2024 10:14:00 +0800
Subject: [PATCH 1/4] [CodeGen][GC] Skip function without GC in
`GCLoweringPass`
---
llvm/lib/CodeGen/GCRootLowering.cpp | 3 +++
1 file changed, 3 insertions(+)
diff --git a/llvm/lib/CodeGen/GCRootLowering.cpp b/llvm/lib/CodeGen/GCRootLowering.cpp
index 894ab9a0486a7b..0d82f2bab8960b 100644
--- a/llvm/lib/CodeGen/GCRootLowering.cpp
+++ b/llvm/lib/CodeGen/GCRootLowering.cpp
@@ -81,6 +81,9 @@ class GCMachineCodeAnalysis : public MachineFunctionPass {
PreservedAnalyses GCLoweringPass::run(Function &F,
FunctionAnalysisManager &FAM) {
+ if (!F.hasGC())
+ return PreservedAnalyses::all();
+
auto &Info = FAM.getResult<GCFunctionAnalysis>(F);
bool Changed = DoLowering(F, Info.getStrategy());
>From 28071e020df3e6634e9f75540401c1e365bf508f Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Fri, 8 Mar 2024 14:16:12 +0800
Subject: [PATCH 2/4] add test
---
llvm/test/CodeGen/Generic/gc-lowering.ll | 42 ++++++++++++++++++++++++
1 file changed, 42 insertions(+)
create mode 100644 llvm/test/CodeGen/Generic/gc-lowering.ll
diff --git a/llvm/test/CodeGen/Generic/gc-lowering.ll b/llvm/test/CodeGen/Generic/gc-lowering.ll
new file mode 100644
index 00000000000000..7297db8c105510
--- /dev/null
+++ b/llvm/test/CodeGen/Generic/gc-lowering.ll
@@ -0,0 +1,42 @@
+; RUN: opt -S -passes='require<collector-metadata>,function(gc-lowering)' < %s | FileCheck %s
+
+declare ptr @llvm_gc_allocate(i32)
+declare void @llvm_gc_initialize(i32)
+
+declare void @llvm.gcroot(ptr, ptr)
+declare void @llvm.gcwrite(ptr, ptr, ptr)
+
+define i32 @main() gc "shadow-stack" {
+entry:
+ %A = alloca ptr
+ %B = alloca ptr
+
+ call void @llvm_gc_initialize(i32 1048576) ; Start with 1MB heap
+
+ ;; ptr A;
+ call void @llvm.gcroot(ptr %A, ptr null)
+
+ ;; A = gcalloc(10);
+ %Aptr = call ptr @llvm_gc_allocate(i32 10)
+ store ptr %Aptr, ptr %A
+
+ ;; ptr B;
+ call void @llvm.gcroot(ptr %B, ptr null)
+
+ ;; B = gcalloc(4);
+ %B.upgrd.1 = call ptr @llvm_gc_allocate(i32 8)
+ store ptr %B.upgrd.1, ptr %B
+
+ ;; *B = A;
+ %B.1 = load ptr, ptr %B
+ %A.1 = load ptr, ptr %A
+ call void @llvm.gcwrite(ptr %A.1, ptr %B.upgrd.1, ptr %B.1)
+ ; CHECK: %A.1 = load ptr, ptr %A, align 8
+ ; CHECK-NEXT: store ptr %A.1, ptr %B.1, align 8
+
+ ret i32 0
+}
+
+define void @no_gc() {
+ ret void
+}
>From ee0bb08209263541a61ce54cf38b8a9a727feecb Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Fri, 8 Mar 2024 21:15:28 +0800
Subject: [PATCH 3/4] convert tab to space
---
llvm/test/CodeGen/Generic/gc-lowering.ll | 40 ++++++++++++------------
1 file changed, 20 insertions(+), 20 deletions(-)
diff --git a/llvm/test/CodeGen/Generic/gc-lowering.ll b/llvm/test/CodeGen/Generic/gc-lowering.ll
index 7297db8c105510..73820ff6256b96 100644
--- a/llvm/test/CodeGen/Generic/gc-lowering.ll
+++ b/llvm/test/CodeGen/Generic/gc-lowering.ll
@@ -8,35 +8,35 @@ declare void @llvm.gcwrite(ptr, ptr, ptr)
define i32 @main() gc "shadow-stack" {
entry:
- %A = alloca ptr
- %B = alloca ptr
+ %A = alloca ptr
+ %B = alloca ptr
- call void @llvm_gc_initialize(i32 1048576) ; Start with 1MB heap
+ call void @llvm_gc_initialize(i32 1048576) ; Start with 1MB heap
- ;; ptr A;
- call void @llvm.gcroot(ptr %A, ptr null)
+ ;; ptr A;
+ call void @llvm.gcroot(ptr %A, ptr null)
- ;; A = gcalloc(10);
- %Aptr = call ptr @llvm_gc_allocate(i32 10)
- store ptr %Aptr, ptr %A
+ ;; A = gcalloc(10);
+ %Aptr = call ptr @llvm_gc_allocate(i32 10)
+ store ptr %Aptr, ptr %A
- ;; ptr B;
- call void @llvm.gcroot(ptr %B, ptr null)
+ ;; ptr B;
+ call void @llvm.gcroot(ptr %B, ptr null)
- ;; B = gcalloc(4);
- %B.upgrd.1 = call ptr @llvm_gc_allocate(i32 8)
- store ptr %B.upgrd.1, ptr %B
+ ;; B = gcalloc(4);
+ %B.upgrd.1 = call ptr @llvm_gc_allocate(i32 8)
+ store ptr %B.upgrd.1, ptr %B
- ;; *B = A;
- %B.1 = load ptr, ptr %B
- %A.1 = load ptr, ptr %A
- call void @llvm.gcwrite(ptr %A.1, ptr %B.upgrd.1, ptr %B.1)
- ; CHECK: %A.1 = load ptr, ptr %A, align 8
- ; CHECK-NEXT: store ptr %A.1, ptr %B.1, align 8
+ ;; *B = A;
+ %B.1 = load ptr, ptr %B
+ %A.1 = load ptr, ptr %A
+ call void @llvm.gcwrite(ptr %A.1, ptr %B.upgrd.1, ptr %B.1)
+ ; CHECK: %A.1 = load ptr, ptr %A, align 8
+ ; CHECK-NEXT: store ptr %A.1, ptr %B.1, align 8
ret i32 0
}
define void @no_gc() {
- ret void
+ ret void
}
>From 0a2d72df8bb14f1f1ad14896ea49d8e7f308f4c9 Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Wed, 13 Mar 2024 20:54:20 +0800
Subject: [PATCH 4/4] use utils/update_test_checks.py
---
llvm/test/CodeGen/Generic/gc-lowering.ll | 24 ++++++++++++++++++++++--
1 file changed, 22 insertions(+), 2 deletions(-)
diff --git a/llvm/test/CodeGen/Generic/gc-lowering.ll b/llvm/test/CodeGen/Generic/gc-lowering.ll
index 73820ff6256b96..fa2e92ab495f4d 100644
--- a/llvm/test/CodeGen/Generic/gc-lowering.ll
+++ b/llvm/test/CodeGen/Generic/gc-lowering.ll
@@ -1,3 +1,4 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -S -passes='require<collector-metadata>,function(gc-lowering)' < %s | FileCheck %s
declare ptr @llvm_gc_allocate(i32)
@@ -7,6 +8,24 @@ declare void @llvm.gcroot(ptr, ptr)
declare void @llvm.gcwrite(ptr, ptr, ptr)
define i32 @main() gc "shadow-stack" {
+; CHECK-LABEL: define i32 @main() gc "shadow-stack" {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[A:%.*]] = alloca ptr, align 8
+; CHECK-NEXT: store ptr null, ptr [[A]], align 8
+; CHECK-NEXT: [[B:%.*]] = alloca ptr, align 8
+; CHECK-NEXT: store ptr null, ptr [[B]], align 8
+; CHECK-NEXT: call void @llvm_gc_initialize(i32 1048576)
+; CHECK-NEXT: call void @llvm.gcroot(ptr [[A]], ptr null)
+; CHECK-NEXT: [[APTR:%.*]] = call ptr @llvm_gc_allocate(i32 10)
+; CHECK-NEXT: store ptr [[APTR]], ptr [[A]], align 8
+; CHECK-NEXT: call void @llvm.gcroot(ptr [[B]], ptr null)
+; CHECK-NEXT: [[B_UPGRD_1:%.*]] = call ptr @llvm_gc_allocate(i32 8)
+; CHECK-NEXT: store ptr [[B_UPGRD_1]], ptr [[B]], align 8
+; CHECK-NEXT: [[B_1:%.*]] = load ptr, ptr [[B]], align 8
+; CHECK-NEXT: [[A_1:%.*]] = load ptr, ptr [[A]], align 8
+; CHECK-NEXT: store ptr [[A_1]], ptr [[B_1]], align 8
+; CHECK-NEXT: ret i32 0
+;
entry:
%A = alloca ptr
%B = alloca ptr
@@ -31,12 +50,13 @@ entry:
%B.1 = load ptr, ptr %B
%A.1 = load ptr, ptr %A
call void @llvm.gcwrite(ptr %A.1, ptr %B.upgrd.1, ptr %B.1)
- ; CHECK: %A.1 = load ptr, ptr %A, align 8
- ; CHECK-NEXT: store ptr %A.1, ptr %B.1, align 8
ret i32 0
}
define void @no_gc() {
+; CHECK-LABEL: define void @no_gc() {
+; CHECK-NEXT: ret void
+;
ret void
}
More information about the llvm-commits
mailing list