llvm.org GIT mirror llvm / 8eaf38e
[CodeExtractor] Use subset of function attributes for extracted function. In addition to target-dependent attributes, we can also preserve a white-listed subset of target independent function attributes. The white-list excludes problematic attributes, most prominently: * attributes related to memory accesses, as alloca instructions could be moved in/out of the extracted block * control-flow dependent attributes, like no_return or thunk, as the relerelevant instructions might or might not get extracted. Thanks @efriedma and @aemerson for providing a set of attributes that cannot be propagated. Reviewers: efriedma, davidxl, davide, silvas Reviewed By: efriedma Differential Revision: https://reviews.llvm.org/D41334 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@321961 91177308-0d34-0410-b5e6-96231b3b80d8 Florian Hahn 1 year, 8 months ago
2 changed file(s) with 160 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
619619 if (oldFunction->hasUWTable())
620620 newFunction->setHasUWTable();
621621
622 // Inherit all of the target dependent attributes.
622 // Inherit all of the target dependent attributes and white-listed
623 // target independent attributes.
623624 // (e.g. If the extracted region contains a call to an x86.sse
624625 // instruction we need to make sure that the extracted region has the
625626 // "target-features" attribute allowing it to be lowered.
626627 // FIXME: This should be changed to check to see if a specific
627628 // attribute can not be inherited.
628 AttrBuilder AB(oldFunction->getAttributes().getFnAttributes());
629 for (const auto &Attr : AB.td_attrs())
630 newFunction->addFnAttr(Attr.first, Attr.second);
631
629 for (const auto &Attr : oldFunction->getAttributes().getFnAttributes()) {
630 if (Attr.isStringAttribute()) {
631 if (Attr.getKindAsString() == "thunk")
632 continue;
633 } else
634 switch (Attr.getKindAsEnum()) {
635 // Those attributes cannot be propagated safely. Explicitly list them
636 // here so we get a warning if new attributes are added. This list also
637 // includes non-function attributes.
638 case Attribute::Alignment:
639 case Attribute::AllocSize:
640 case Attribute::ArgMemOnly:
641 case Attribute::Builtin:
642 case Attribute::ByVal:
643 case Attribute::Convergent:
644 case Attribute::Dereferenceable:
645 case Attribute::DereferenceableOrNull:
646 case Attribute::InAlloca:
647 case Attribute::InReg:
648 case Attribute::InaccessibleMemOnly:
649 case Attribute::InaccessibleMemOrArgMemOnly:
650 case Attribute::JumpTable:
651 case Attribute::Naked:
652 case Attribute::Nest:
653 case Attribute::NoAlias:
654 case Attribute::NoBuiltin:
655 case Attribute::NoCapture:
656 case Attribute::NoReturn:
657 case Attribute::None:
658 case Attribute::NonNull:
659 case Attribute::ReadNone:
660 case Attribute::ReadOnly:
661 case Attribute::Returned:
662 case Attribute::ReturnsTwice:
663 case Attribute::SExt:
664 case Attribute::Speculatable:
665 case Attribute::StackAlignment:
666 case Attribute::StructRet:
667 case Attribute::SwiftError:
668 case Attribute::SwiftSelf:
669 case Attribute::WriteOnly:
670 case Attribute::ZExt:
671 case Attribute::EndAttrKinds:
672 continue;
673 // Those attributes should be safe to propagate to the extracted function.
674 case Attribute::AlwaysInline:
675 case Attribute::Cold:
676 case Attribute::NoRecurse:
677 case Attribute::InlineHint:
678 case Attribute::MinSize:
679 case Attribute::NoDuplicate:
680 case Attribute::NoImplicitFloat:
681 case Attribute::NoInline:
682 case Attribute::NonLazyBind:
683 case Attribute::NoRedZone:
684 case Attribute::NoUnwind:
685 case Attribute::OptimizeNone:
686 case Attribute::OptimizeForSize:
687 case Attribute::SafeStack:
688 case Attribute::SanitizeAddress:
689 case Attribute::SanitizeMemory:
690 case Attribute::SanitizeThread:
691 case Attribute::SanitizeHWAddress:
692 case Attribute::StackProtect:
693 case Attribute::StackProtectReq:
694 case Attribute::StackProtectStrong:
695 case Attribute::StrictFP:
696 case Attribute::UWTable:
697 break;
698 }
699
700 newFunction->addFnAttr(Attr);
701 }
632702 newFunction->getBasicBlockList().push_back(newRootNode);
633703
634704 // Create an iterator to name all of the arguments we inserted.
0 ; RUN: opt < %s -S -partial-inliner -skip-partial-inlining-cost-analysis=true | FileCheck %s
1
2
3 define i32 @callee_most(i32 %v) unnamed_addr #0 #1 {
4 entry:
5 %cmp = icmp sgt i32 %v, 2000
6 br i1 %cmp, label %if.then, label %if.end
7
8 if.then:
9 br label %if.then2
10
11 if.then2:
12 %sub = sub i32 %v, 10
13 br label %if.end
14
15 if.end:
16 %v2 = phi i32 [ %v, %entry ], [ %sub, %if.then2 ]
17 %add = add nsw i32 %v2, 200
18 ret i32 %add
19 }
20
21 define i32 @callee_noinline(i32 %v) optnone noinline {
22 entry:
23 %cmp = icmp sgt i32 %v, 2000
24 br i1 %cmp, label %if.then, label %if.end
25
26 if.then:
27 br label %if.then2
28
29 if.then2:
30 %sub = sub i32 %v, 10
31 br label %if.end
32
33 if.end:
34 %v2 = phi i32 [ %v, %entry ], [ %sub, %if.then2 ]
35 %add = add nsw i32 %v2, 200
36 ret i32 %add
37 }
38
39 define i32 @callee_writeonly(i32 %v) writeonly {
40 entry:
41 %cmp = icmp sgt i32 %v, 2000
42 br i1 %cmp, label %if.then, label %if.end
43
44 if.then:
45 br label %if.then2
46
47 if.then2:
48 %sub = sub i32 %v, 10
49 br label %if.end
50
51 if.end:
52 %v2 = phi i32 [ %v, %entry ], [ %sub, %if.then2 ]
53 %add = add nsw i32 %v2, 200
54 ret i32 %add
55 }
56 ; CHECK-LABEL: @caller
57 ; CHECK: call void @callee_most.2_if.then(i32 %v
58 ; CHECK: call i32 @callee_noinline(i32 %v)
59 ; CHECK: call void @callee_writeonly.1_if.then(i32 %v
60 define i32 @caller(i32 %v) {
61 entry:
62 %c1 = call i32 @callee_most(i32 %v)
63 %c2 = call i32 @callee_noinline(i32 %v)
64 %c3 = call i32 @callee_writeonly(i32 %v)
65 ret i32 %c3
66 }
67
68 ; CHECK: define internal void @callee_writeonly.1_if.then(i32 %v, i32* %sub.out) {
69 ; CHECK: define internal void @callee_most.2_if.then(i32 %v, i32* %sub.out) [[FN_ATTRS:#[0-9]+]]
70
71 ; attributes to preserve
72 attributes #0 = {
73 inlinehint minsize noduplicate noimplicitfloat norecurse noredzone nounwind
74 nonlazybind optsize safestack sanitize_address sanitize_hwaddress sanitize_memory
75 sanitize_thread ssp sspreq sspstrong strictfp uwtable "foo"="bar"
76 "patchable-function"="prologue-short-redirect" "probe-stack"="_foo_guard" "stack-probe-size"="4096" }
77
78 ; CHECK: attributes [[FN_ATTRS]] = { inlinehint minsize noduplicate noimplicitfloat norecurse noredzone nounwind nonlazybind optsize safestack sanitize_address sanitize_hwaddress sanitize_memory sanitize_thread ssp sspreq sspstrong strictfp uwtable "foo"="bar" "patchable-function"="prologue-short-redirect" "probe-stack"="_foo_guard" "stack-probe-size"="4096" }
79
80 ; attributes to drop
81 attributes #1 = {
82 alignstack=16 convergent inaccessiblememonly inaccessiblemem_or_argmemonly naked
83 noreturn readonly argmemonly returns_twice speculatable "thunk"
84 }