diff --git a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp index a477c90bb4f45..a8f37448f0c38 100644 --- a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp +++ b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp @@ -2902,6 +2902,22 @@ struct AANonConvergentImpl : public AANonConvergent { } }; +static bool FunctionCalledWithConvergenceToken(const Function *F) { + for (auto &Use : F->uses()) { + CallBase *CB = dyn_cast(Use.getUser()); + if (!CB) + continue; + + // We are not called, just used as an argument. + if (CB->getCalledFunction() != F) + continue; + + if (CB->getConvergenceControlToken()) + return true; + } + return false; +} + struct AANonConvergentFunction final : AANonConvergentImpl { AANonConvergentFunction(const IRPosition &IRP, Attributor &A) : AANonConvergentImpl(IRP, A) {} @@ -2929,6 +2945,11 @@ struct AANonConvergentFunction final : AANonConvergentImpl { UsedAssumedInformation)) { return indicatePessimisticFixpoint(); } + + const Function *F = this->getIRPosition().getAssociatedFunction(); + if (FunctionCalledWithConvergenceToken(F)) + return indicatePessimisticFixpoint(); + return ChangeStatus::UNCHANGED; } diff --git a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp index ef7989507c89f..dc980a4a997b5 100644 --- a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp +++ b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp @@ -1864,6 +1864,23 @@ static bool InstrBreaksNonConvergent(Instruction &I, !SCCNodes.contains(CB->getCalledFunction()); } +static bool FunctionRequiresConvergence(const Function *F) { + for (auto &Use : F->uses()) { + CallBase *CB = dyn_cast(Use.getUser()); + if (!CB) + return true; + + // We are not called, just used as an argument. + if (CB->getCalledFunction() != F) + continue; + + if (CB->getConvergenceControlToken()) + return true; + } + + return false; +} + /// Helper for NoUnwind inference predicate InstrBreaksAttribute. static bool InstrBreaksNonThrowing(Instruction &I, const SCCNodeSet &SCCNodes) { if (!I.mayThrow(/* IncludePhaseOneUnwind */ true)) @@ -1967,7 +1984,9 @@ static void inferConvergent(const SCCNodeSet &SCCNodes, AI.registerAttrInference(AttributeInferer::InferenceDescriptor{ Attribute::Convergent, // Skip non-convergent functions. - [](const Function &F) { return !F.isConvergent(); }, + [](const Function &F) { + return !F.isConvergent() || FunctionRequiresConvergence(&F); + }, // Instructions that break non-convergent assumption. [SCCNodes](Instruction &I) { return InstrBreaksNonConvergent(I, SCCNodes); diff --git a/llvm/test/Transforms/ADCE/convergence.ll b/llvm/test/Transforms/ADCE/convergence.ll new file mode 100644 index 0000000000000..5d7b9bd65c821 --- /dev/null +++ b/llvm/test/Transforms/ADCE/convergence.ll @@ -0,0 +1,92 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 +; RUN: opt %s -passes=adce -S | FileCheck %s + +; CHECK: Function Attrs: convergent +define i32 @foo(i32 %a) #0 { +; CHECK-LABEL: define i32 @foo( +; CHECK-SAME: i32 [[A:%.*]]) #[[ATTR0:[0-9]+]] { +; CHECK-NEXT: [[ENTRY:.*:]] +; CHECK-NEXT: ret i32 [[A]] +; +entry: + %tk = call token @llvm.experimental.convergence.entry() + ret i32 %a +} + +; CHECK: Function Attrs: convergent +define void @bar() #0 { +; CHECK-LABEL: define void @bar( +; CHECK-SAME: ) #[[ATTR0]] { +; CHECK-NEXT: [[ENTRY:.*:]] +; CHECK-NEXT: ret void +; +entry: + %tk = call token @llvm.experimental.convergence.anchor() + ret void +} + +; CHECK: Function Attrs: convergent +define void @baz() #0 { +; CHECK-LABEL: define void @baz( +; CHECK-SAME: ) #[[ATTR0]] { +; CHECK-NEXT: [[ENTRY:.*:]] +; CHECK-NEXT: br label %[[HEADER:.*]] +; CHECK: [[HEADER]]: +; CHECK-NEXT: br i1 true, label %[[BODY:.*]], label %[[EXIT:.*]] +; CHECK: [[BODY]]: +; CHECK-NEXT: br label %[[HEADER]] +; CHECK: [[EXIT]]: +; CHECK-NEXT: ret void +; +entry: + %tk0 = call token @llvm.experimental.convergence.entry() + br label %header + +header: + %tk1 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %tk0) ] + br i1 true, label %body, label %exit + +body: + br label %header + +exit: + ret void +} + +define void @indirect_inner() #0 { +; CHECK-LABEL: define void @indirect_inner( +; CHECK-SAME: ) #[[ATTR0]] { +; CHECK-NEXT: [[ENTRY:.*:]] +; CHECK-NEXT: ret void +; +entry: + %tk0 = call token @llvm.experimental.convergence.entry() + ret void +} + +define void @indirect() #0 { +; CHECK-LABEL: define void @indirect( +; CHECK-SAME: ) #[[ATTR0]] { +; CHECK-NEXT: [[ENTRY:.*:]] +; CHECK-NEXT: [[TK0:%.*]] = call token @llvm.experimental.convergence.entry() +; CHECK-NEXT: [[VAR:%.*]] = alloca ptr, align 8 +; CHECK-NEXT: store ptr @indirect_inner, ptr [[VAR]], align 8 +; CHECK-NEXT: [[PTR:%.*]] = load ptr, ptr [[VAR]], align 8 +; CHECK-NEXT: call void [[PTR]]() #[[ATTR0]] [ "convergencectrl"(token [[TK0]]) ] +; CHECK-NEXT: ret void +; +entry: + %tk0 = call token @llvm.experimental.convergence.entry() + %var = alloca ptr, align 8 + store ptr @indirect_inner, ptr %var, align 8 + %ptr = load ptr, ptr %var, align 8 + call void %ptr() convergent [ "convergencectrl"(token %tk0) ] + ret void +} + +declare token @llvm.experimental.convergence.entry() #1 +declare token @llvm.experimental.convergence.anchor() #1 +declare token @llvm.experimental.convergence.loop() #1 + +attributes #0 = { convergent } +attributes #1 = { convergent nocallback nofree nosync nounwind willreturn memory(none) } diff --git a/llvm/test/Transforms/Attributor/convergent.ll b/llvm/test/Transforms/Attributor/convergent.ll index 702d0bb0a9e63..f4ae905f4bd14 100644 --- a/llvm/test/Transforms/Attributor/convergent.ll +++ b/llvm/test/Transforms/Attributor/convergent.ll @@ -1,11 +1,11 @@ -; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-attributes --check-globals -; RUN: opt -aa-pipeline=basic-aa -passes=attributor -attributor-manifest-internal -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK,TUNIT -; RUN: opt -aa-pipeline=basic-aa -passes=attributor-cgscc -attributor-manifest-internal -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK,CGSCC +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-attributes --check-globals all --version 5 +; RUN: opt -aa-pipeline=basic-aa -passes=attributor -attributor-manifest-internal -attributor-annotate-decl-cs -S %s | FileCheck %s --check-prefixes=CHECK,TUNIT +; RUN: opt -aa-pipeline=basic-aa -passes=attributor-cgscc -attributor-manifest-internal -attributor-annotate-decl-cs -S %s | FileCheck %s --check-prefixes=CHECK,CGSCC define i32 @defined() convergent { ; CHECK: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) -; CHECK-LABEL: define {{[^@]+}}@defined -; CHECK-SAME: () #[[ATTR0:[0-9]+]] { +; CHECK-LABEL: define noundef i32 @defined( +; CHECK-SAME: ) #[[ATTR0:[0-9]+]] { ; CHECK-NEXT: ret i32 1 ; ret i32 1 @@ -13,14 +13,14 @@ define i32 @defined() convergent { define i32 @calls_defined() convergent { ; TUNIT: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) -; TUNIT-LABEL: define {{[^@]+}}@calls_defined -; TUNIT-SAME: () #[[ATTR0]] { +; TUNIT-LABEL: define noundef i32 @calls_defined( +; TUNIT-SAME: ) #[[ATTR0]] { ; TUNIT-NEXT: ret i32 1 ; ; CGSCC: Function Attrs: convergent mustprogress nofree nosync nounwind willreturn memory(none) -; CGSCC-LABEL: define {{[^@]+}}@calls_defined -; CGSCC-SAME: () #[[ATTR1:[0-9]+]] { -; CGSCC-NEXT: [[A:%.*]] = call noundef i32 @defined() #[[ATTR6:[0-9]+]] +; CGSCC-LABEL: define noundef i32 @calls_defined( +; CGSCC-SAME: ) #[[ATTR1:[0-9]+]] { +; CGSCC-NEXT: [[A:%.*]] = call noundef i32 @defined() #[[ATTR7:[0-9]+]] ; CGSCC-NEXT: ret i32 [[A]] ; %a = call i32 @defined() @@ -30,7 +30,7 @@ define i32 @calls_defined() convergent { declare void @declared_non_convergent() define void @calls_declared_non_convergent() convergent { -; CHECK-LABEL: define {{[^@]+}}@calls_declared_non_convergent() { +; CHECK-LABEL: define void @calls_declared_non_convergent() { ; CHECK-NEXT: call void @declared_non_convergent() ; CHECK-NEXT: ret void ; @@ -43,14 +43,14 @@ declare i32 @declared_convergent() convergent define i32 @calls_declared_convergent() convergent { ; TUNIT: Function Attrs: convergent -; TUNIT-LABEL: define {{[^@]+}}@calls_declared_convergent -; TUNIT-SAME: () #[[ATTR1:[0-9]+]] { +; TUNIT-LABEL: define i32 @calls_declared_convergent( +; TUNIT-SAME: ) #[[ATTR1:[0-9]+]] { ; TUNIT-NEXT: [[A:%.*]] = call i32 @declared_convergent() ; TUNIT-NEXT: ret i32 [[A]] ; ; CGSCC: Function Attrs: convergent -; CGSCC-LABEL: define {{[^@]+}}@calls_declared_convergent -; CGSCC-SAME: () #[[ATTR2:[0-9]+]] { +; CGSCC-LABEL: define i32 @calls_declared_convergent( +; CGSCC-SAME: ) #[[ATTR2:[0-9]+]] { ; CGSCC-NEXT: [[A:%.*]] = call i32 @declared_convergent() ; CGSCC-NEXT: ret i32 [[A]] ; @@ -58,9 +58,120 @@ define i32 @calls_declared_convergent() convergent { ret i32 %a } +; Function declared convergent. Body not provided to prevent inlining +; of the functions below. +declare i32 @direct_call_convergent_attribute_with_token_declared(i32 %a) + +; This function could be declared non-convergent as it only calls +; non-convergent functions, but it is being called with a convergence token, +; hence it is not allowed to drop the convergence attribute. +define i32 @direct_call_convergent_attribute_with_token_inner(i32 %a) convergent { +; TUNIT: Function Attrs: convergent +; TUNIT-LABEL: define i32 @direct_call_convergent_attribute_with_token_inner( +; TUNIT-SAME: i32 [[A:%.*]]) #[[ATTR1]] { +; TUNIT-NEXT: [[RS:%.*]] = call i32 @direct_call_convergent_attribute_with_token_declared(i32 [[A]]) +; TUNIT-NEXT: ret i32 [[RS]] +; +; CGSCC: Function Attrs: convergent +; CGSCC-LABEL: define i32 @direct_call_convergent_attribute_with_token_inner( +; CGSCC-SAME: i32 [[A:%.*]]) #[[ATTR2]] { +; CGSCC-NEXT: [[RS:%.*]] = call i32 @direct_call_convergent_attribute_with_token_declared(i32 [[A]]) +; CGSCC-NEXT: ret i32 [[RS]] +; + %rs = call i32 @direct_call_convergent_attribute_with_token_declared(i32 %a) + ret i32 %rs +} + +define i32 @direct_call_convergent_attribute_with_token(i32 %a) convergent { +; TUNIT: Function Attrs: convergent +; TUNIT-LABEL: define i32 @direct_call_convergent_attribute_with_token( +; TUNIT-SAME: i32 [[A:%.*]]) #[[ATTR1]] { +; TUNIT-NEXT: [[TK:%.*]] = call token @llvm.experimental.convergence.entry() #[[ATTR6:[0-9]+]] +; TUNIT-NEXT: [[RS:%.*]] = call i32 @direct_call_convergent_attribute_with_token_inner(i32 [[A]]) [ "convergencectrl"(token [[TK]]) ] +; TUNIT-NEXT: ret i32 [[RS]] +; +; CGSCC: Function Attrs: convergent +; CGSCC-LABEL: define i32 @direct_call_convergent_attribute_with_token( +; CGSCC-SAME: i32 [[A:%.*]]) #[[ATTR2]] { +; CGSCC-NEXT: [[TK:%.*]] = call token @llvm.experimental.convergence.entry() #[[ATTR8:[0-9]+]] +; CGSCC-NEXT: [[RS:%.*]] = call i32 @direct_call_convergent_attribute_with_token_inner(i32 [[A]]) [ "convergencectrl"(token [[TK]]) ] +; CGSCC-NEXT: ret i32 [[RS]] +; + %tk = call token @llvm.experimental.convergence.entry() + %rs = call i32 @direct_call_convergent_attribute_with_token_inner(i32 %a) [ "convergencectrl"(token %tk) ] + ret i32 %rs +} + +; Function declared non-convergent. +declare i32 @indirect_call_declared(i32 %a) + +; Function declared convergent, but which is not required to be +; marked convergent: the only used is through a call which is marked +; as convergent, hence the function attribute can be dropped. +define i32 @indirect_call_inner(i32 %a) convergent { +; CHECK-LABEL: define i32 @indirect_call_inner( +; CHECK-SAME: i32 [[A:%.*]]) { +; CHECK-NEXT: [[RS:%.*]] = call i32 @indirect_call_declared(i32 [[A]]) +; CHECK-NEXT: ret i32 [[RS]] +; + %rs = call i32 @indirect_call_declared(i32 %a) + ret i32 %rs +} + +; Convergent function, calling a function and marking the call as convergent. +define i32 @indirect_call(i32 %a) convergent { +; TUNIT: Function Attrs: convergent +; TUNIT-LABEL: define i32 @indirect_call( +; TUNIT-SAME: i32 [[A:%.*]]) #[[ATTR1]] { +; TUNIT-NEXT: [[TK:%.*]] = call token @llvm.experimental.convergence.entry() #[[ATTR7:[0-9]+]] +; TUNIT-NEXT: [[RS:%.*]] = call i32 @indirect_call_inner(i32 [[A]]) #[[ATTR1]] [ "convergencectrl"(token [[TK]]) ] +; TUNIT-NEXT: ret i32 [[RS]] +; +; CGSCC: Function Attrs: convergent +; CGSCC-LABEL: define i32 @indirect_call( +; CGSCC-SAME: i32 [[A:%.*]]) #[[ATTR2]] { +; CGSCC-NEXT: [[TK:%.*]] = call token @llvm.experimental.convergence.entry() #[[ATTR7]] +; CGSCC-NEXT: [[RS:%.*]] = call i32 @indirect_call_inner(i32 [[A]]) #[[ATTR2]] [ "convergencectrl"(token [[TK]]) ] +; CGSCC-NEXT: ret i32 [[RS]] +; + %tk = call token @llvm.experimental.convergence.entry() + %fptr = alloca ptr + store ptr @indirect_call_inner, ptr %fptr + %ic = load ptr, ptr %fptr + %rs = call i32 %ic(i32 %a) convergent [ "convergencectrl"(token %tk) ] + ret i32 %rs +} + +; A non-convergent declaration. +declare i32 @leaf_convergent_indirect_declared(i32 %a) + +; A function calling indirectly a non-convergent function. +define i32 @leaf_convergent_indirect(i32 %a) convergent { +; TUNIT: Function Attrs: convergent +; TUNIT-LABEL: define i32 @leaf_convergent_indirect( +; TUNIT-SAME: i32 [[A:%.*]]) #[[ATTR1]] { +; TUNIT-NEXT: [[TK:%.*]] = call token @llvm.experimental.convergence.entry() #[[ATTR7]] +; TUNIT-NEXT: [[RS:%.*]] = call i32 @leaf_convergent_indirect_declared(i32 [[A]]) #[[ATTR1]] [ "convergencectrl"(token [[TK]]) ] +; TUNIT-NEXT: ret i32 [[RS]] +; +; CGSCC: Function Attrs: convergent +; CGSCC-LABEL: define i32 @leaf_convergent_indirect( +; CGSCC-SAME: i32 [[A:%.*]]) #[[ATTR2]] { +; CGSCC-NEXT: [[TK:%.*]] = call token @llvm.experimental.convergence.entry() #[[ATTR7]] +; CGSCC-NEXT: [[RS:%.*]] = call i32 @leaf_convergent_indirect_declared(i32 [[A]]) #[[ATTR2]] [ "convergencectrl"(token [[TK]]) ] +; CGSCC-NEXT: ret i32 [[RS]] +; + %tk = call token @llvm.experimental.convergence.entry() + %fptr = alloca ptr + store ptr @leaf_convergent_indirect_declared, ptr %fptr + %ic = load ptr, ptr %fptr + %rs = call i32 %ic(i32 %a) convergent [ "convergencectrl"(token %tk) ] + ret i32 %rs +} + define i32 @defined_with_asm(i32 %a, i32 %b) { -; CHECK-LABEL: define {{[^@]+}}@defined_with_asm -; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]]) { +; CHECK-LABEL: define i32 @defined_with_asm( +; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) { ; CHECK-NEXT: [[RESULT:%.*]] = add i32 [[A]], [[B]] ; CHECK-NEXT: [[ASM_RESULT:%.*]] = call i32 asm sideeffect "addl $1, $0", "=r,r"(i32 [[RESULT]]) ; CHECK-NEXT: ret i32 [[ASM_RESULT]] @@ -72,14 +183,14 @@ define i32 @defined_with_asm(i32 %a, i32 %b) { define i32 @calls_defined_with_asm(i32 %a, i32 %b) convergent { ; TUNIT: Function Attrs: convergent -; TUNIT-LABEL: define {{[^@]+}}@calls_defined_with_asm -; TUNIT-SAME: (i32 [[A:%.*]], i32 [[B:%.*]]) #[[ATTR1]] { +; TUNIT-LABEL: define i32 @calls_defined_with_asm( +; TUNIT-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) #[[ATTR1]] { ; TUNIT-NEXT: [[C:%.*]] = call i32 @defined_with_asm(i32 [[A]], i32 [[B]]) ; TUNIT-NEXT: ret i32 [[C]] ; ; CGSCC: Function Attrs: convergent -; CGSCC-LABEL: define {{[^@]+}}@calls_defined_with_asm -; CGSCC-SAME: (i32 [[A:%.*]], i32 [[B:%.*]]) #[[ATTR2]] { +; CGSCC-LABEL: define i32 @calls_defined_with_asm( +; CGSCC-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) #[[ATTR2]] { ; CGSCC-NEXT: [[C:%.*]] = call i32 @defined_with_asm(i32 [[A]], i32 [[B]]) ; CGSCC-NEXT: ret i32 [[C]] ; @@ -91,15 +202,15 @@ declare void @llvm.convergent.copy.p0.p0.i64(ptr %dest, ptr %src, i64 %size, i1 define void @calls_convergent_intrinsic(ptr %dest, ptr %src, i64 %size) convergent { ; TUNIT: Function Attrs: convergent mustprogress nofree nosync nounwind willreturn memory(argmem: readwrite) -; TUNIT-LABEL: define {{[^@]+}}@calls_convergent_intrinsic -; TUNIT-SAME: (ptr nofree [[DEST:%.*]], ptr nofree [[SRC:%.*]], i64 [[SIZE:%.*]]) #[[ATTR3:[0-9]+]] { -; TUNIT-NEXT: call void @llvm.convergent.copy.p0.p0.i64(ptr nofree [[DEST]], ptr nofree [[SRC]], i64 [[SIZE]], i1 noundef false) #[[ATTR5:[0-9]+]] +; TUNIT-LABEL: define void @calls_convergent_intrinsic( +; TUNIT-SAME: ptr nofree [[DEST:%.*]], ptr nofree [[SRC:%.*]], i64 [[SIZE:%.*]]) #[[ATTR3:[0-9]+]] { +; TUNIT-NEXT: call void @llvm.convergent.copy.p0.p0.i64(ptr nofree [[DEST]], ptr nofree [[SRC]], i64 [[SIZE]], i1 noundef false) #[[ATTR6]] ; TUNIT-NEXT: ret void ; ; CGSCC: Function Attrs: convergent mustprogress nofree nosync nounwind willreturn memory(argmem: readwrite) -; CGSCC-LABEL: define {{[^@]+}}@calls_convergent_intrinsic -; CGSCC-SAME: (ptr nofree [[DEST:%.*]], ptr nofree [[SRC:%.*]], i64 [[SIZE:%.*]]) #[[ATTR4:[0-9]+]] { -; CGSCC-NEXT: call void @llvm.convergent.copy.p0.p0.i64(ptr nofree [[DEST]], ptr nofree [[SRC]], i64 [[SIZE]], i1 noundef false) #[[ATTR7:[0-9]+]] +; CGSCC-LABEL: define void @calls_convergent_intrinsic( +; CGSCC-SAME: ptr nofree [[DEST:%.*]], ptr nofree [[SRC:%.*]], i64 [[SIZE:%.*]]) #[[ATTR4:[0-9]+]] { +; CGSCC-NEXT: call void @llvm.convergent.copy.p0.p0.i64(ptr nofree [[DEST]], ptr nofree [[SRC]], i64 [[SIZE]], i1 noundef false) #[[ATTR8]] ; CGSCC-NEXT: ret void ; call void @llvm.convergent.copy.p0.p0.i64(ptr %dest, ptr %src, i64 %size, i1 false) @@ -110,15 +221,15 @@ declare void @llvm.memcpy.p0.p0.i64(ptr %dest, ptr %src, i64 %size, i1 %isVolati define void @calls_intrinsic(ptr %dest, ptr %src, i64 %size) convergent { ; TUNIT: Function Attrs: convergent mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) -; TUNIT-LABEL: define {{[^@]+}}@calls_intrinsic -; TUNIT-SAME: (ptr nofree writeonly captures(none) [[DEST:%.*]], ptr nofree readonly captures(none) [[SRC:%.*]], i64 [[SIZE:%.*]]) #[[ATTR2:[0-9]+]] { -; TUNIT-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr nofree writeonly captures(none) [[DEST]], ptr nofree readonly captures(none) [[SRC]], i64 [[SIZE]], i1 noundef false) #[[ATTR5]] +; TUNIT-LABEL: define void @calls_intrinsic( +; TUNIT-SAME: ptr nofree writeonly captures(none) [[DEST:%.*]], ptr nofree readonly captures(none) [[SRC:%.*]], i64 [[SIZE:%.*]]) #[[ATTR2:[0-9]+]] { +; TUNIT-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr nofree writeonly captures(none) [[DEST]], ptr nofree readonly captures(none) [[SRC]], i64 [[SIZE]], i1 noundef false) #[[ATTR6]] ; TUNIT-NEXT: ret void ; ; CGSCC: Function Attrs: convergent mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) -; CGSCC-LABEL: define {{[^@]+}}@calls_intrinsic -; CGSCC-SAME: (ptr nofree writeonly captures(none) [[DEST:%.*]], ptr nofree readonly captures(none) [[SRC:%.*]], i64 [[SIZE:%.*]]) #[[ATTR3:[0-9]+]] { -; CGSCC-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr nofree writeonly captures(none) [[DEST]], ptr nofree readonly captures(none) [[SRC]], i64 [[SIZE]], i1 noundef false) #[[ATTR7]] +; CGSCC-LABEL: define void @calls_intrinsic( +; CGSCC-SAME: ptr nofree writeonly captures(none) [[DEST:%.*]], ptr nofree readonly captures(none) [[SRC:%.*]], i64 [[SIZE:%.*]]) #[[ATTR3:[0-9]+]] { +; CGSCC-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr nofree writeonly captures(none) [[DEST]], ptr nofree readonly captures(none) [[SRC]], i64 [[SIZE]], i1 noundef false) #[[ATTR8]] ; CGSCC-NEXT: ret void ; call void @llvm.memcpy.p0.p0.i64(ptr %dest, ptr %src, i64 %size, i1 false) @@ -133,7 +244,9 @@ attributes #0 = { convergent mustprogress nofree norecurse nosync nounwind willr ; TUNIT: attributes #[[ATTR2]] = { convergent mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) } ; TUNIT: attributes #[[ATTR3]] = { convergent mustprogress nofree nosync nounwind willreturn memory(argmem: readwrite) } ; TUNIT: attributes #[[ATTR4:[0-9]+]] = { nocallback nofree nounwind willreturn memory(argmem: readwrite) } -; TUNIT: attributes #[[ATTR5]] = { nofree willreturn } +; TUNIT: attributes #[[ATTR5:[0-9]+]] = { convergent nocallback nofree nosync nounwind willreturn memory(none) } +; TUNIT: attributes #[[ATTR6]] = { nofree willreturn } +; TUNIT: attributes #[[ATTR7]] = { nofree nosync willreturn } ;. ; CGSCC: attributes #[[ATTR0]] = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) } ; CGSCC: attributes #[[ATTR1]] = { convergent mustprogress nofree nosync nounwind willreturn memory(none) } @@ -141,6 +254,7 @@ attributes #0 = { convergent mustprogress nofree norecurse nosync nounwind willr ; CGSCC: attributes #[[ATTR3]] = { convergent mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) } ; CGSCC: attributes #[[ATTR4]] = { convergent mustprogress nofree nosync nounwind willreturn memory(argmem: readwrite) } ; CGSCC: attributes #[[ATTR5:[0-9]+]] = { nocallback nofree nounwind willreturn memory(argmem: readwrite) } -; CGSCC: attributes #[[ATTR6]] = { nofree nosync willreturn } -; CGSCC: attributes #[[ATTR7]] = { nofree willreturn } +; CGSCC: attributes #[[ATTR6:[0-9]+]] = { convergent nocallback nofree nosync nounwind willreturn memory(none) } +; CGSCC: attributes #[[ATTR7]] = { nofree nosync willreturn } +; CGSCC: attributes #[[ATTR8]] = { nofree willreturn } ;. diff --git a/llvm/test/Transforms/BDCE/convergence.ll b/llvm/test/Transforms/BDCE/convergence.ll new file mode 100644 index 0000000000000..51f279925b9f7 --- /dev/null +++ b/llvm/test/Transforms/BDCE/convergence.ll @@ -0,0 +1,62 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 +; RUN: opt %s -passes=bdce -S | FileCheck %s + +; CHECK: Function Attrs: convergent +define i32 @foo(i32 %a) #0 { +; CHECK-LABEL: define i32 @foo( +; CHECK-SAME: i32 [[A:%.*]]) #[[ATTR0:[0-9]+]] { +; CHECK-NEXT: [[ENTRY:.*:]] +; CHECK-NEXT: ret i32 [[A]] +; +entry: + %tk0 = call token @llvm.experimental.convergence.entry() + ret i32 %a +} + +; CHECK: Function Attrs: convergent +define void @bar() #0 { +; CHECK-LABEL: define void @bar( +; CHECK-SAME: ) #[[ATTR0]] { +; CHECK-NEXT: [[ENTRY:.*:]] +; CHECK-NEXT: ret void +; +entry: + %tk0 = call token @llvm.experimental.convergence.anchor() + ret void +} + +; CHECK: Function Attrs: convergent +define void @baz() #0 { +; CHECK-LABEL: define void @baz( +; CHECK-SAME: ) #[[ATTR0]] { +; CHECK-NEXT: [[ENTRY:.*:]] +; CHECK-NEXT: br label %[[HEADER:.*]] +; CHECK: [[HEADER]]: +; CHECK-NEXT: br i1 true, label %[[BODY:.*]], label %[[EXIT:.*]] +; CHECK: [[BODY]]: +; CHECK-NEXT: br label %[[HEADER]] +; CHECK: [[EXIT]]: +; CHECK-NEXT: ret void +; +entry: + %tk0 = call token @llvm.experimental.convergence.entry() + br label %header + +header: + %tk1 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %tk0) ] + br i1 true, label %body, label %exit + +body: + br label %header + +exit: + ret void +} + +declare token @llvm.experimental.convergence.entry() #1 +declare token @llvm.experimental.convergence.anchor() #1 +declare token @llvm.experimental.convergence.loop() #1 + +attributes #0 = { convergent } +attributes #1 = { convergent nocallback nofree nosync nounwind willreturn memory(none) } + diff --git a/llvm/test/Transforms/FunctionAttrs/convergent.ll b/llvm/test/Transforms/FunctionAttrs/convergent.ll index fe8029d39d924..20292a01febae 100644 --- a/llvm/test/Transforms/FunctionAttrs/convergent.ll +++ b/llvm/test/Transforms/FunctionAttrs/convergent.ll @@ -1,5 +1,7 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-attributes ; RUN: opt -passes=function-attrs -S < %s | FileCheck %s +; RUN: opt -passes=function-attrs -S < %s | FileCheck %s +; RUN: opt -passes=function-attrs --aa-pipeline=basic-aa -S %s | FileCheck %s define i32 @nonleaf() convergent { ; CHECK: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) @@ -129,3 +131,35 @@ define i32 @noopt_friend() convergent { %a = call i32 @noopt() ret i32 0 } + +; A function which should normally be stripped of its convergent attribute, +; but because it's used in a controlled convergence call, the attribute +; remains. +; This could be improved by propagating the non-convergence outside of the +; function, but for the time being, we stop when we encounter this scenario. +define i32 @leaf_noconvergent_used() convergent { +; CHECK: Function Attrs: convergent mustprogress nofree norecurse nosync nounwind willreturn memory(none) +; CHECK-LABEL: define {{[^@]+}}@leaf_noconvergent_used +; CHECK-SAME: () #[[ATTR7:[0-9]+]] { +; CHECK-NEXT: ret i32 0 +; + ret i32 0 +} + +define i32 @nonleaf_convergent() convergent { +; CHECK: Function Attrs: convergent mustprogress nofree norecurse nosync nounwind willreturn memory(none) +; CHECK-LABEL: define {{[^@]+}}@nonleaf_convergent +; CHECK-SAME: () #[[ATTR7]] { +; CHECK-NEXT: [[TMP1:%.*]] = call token @llvm.experimental.convergence.entry() +; CHECK-NEXT: [[TMP2:%.*]] = call i32 @leaf_noconvergent_used() [ "convergencectrl"(token [[TMP1]]) ] +; CHECK-NEXT: ret i32 0 +; + %1 = call token @llvm.experimental.convergence.entry() + %2 = call i32 @leaf_noconvergent_used() [ "convergencectrl"(token %1) ] + ret i32 0 +} + + +declare token @llvm.experimental.convergence.entry() #1 + +attributes #1 = { convergent nocallback nofree nosync nounwind willreturn memory(none) }