@@ -1620,15 +1620,113 @@ static void AddAttributesFromFunctionProtoType(ASTContext &Ctx,
1620
1620
FuncAttrs.addAttribute (llvm::Attribute::NoUnwind);
1621
1621
}
1622
1622
1623
+ void CodeGenModule::ConstructDefaultFnAttrList (StringRef Name, bool HasOptnone,
1624
+ bool AttrOnCallSite,
1625
+ llvm::AttrBuilder &FuncAttrs) {
1626
+ // OptimizeNoneAttr takes precedence over -Os or -Oz. No warning needed.
1627
+ if (!HasOptnone) {
1628
+ if (CodeGenOpts.OptimizeSize )
1629
+ FuncAttrs.addAttribute (llvm::Attribute::OptimizeForSize);
1630
+ if (CodeGenOpts.OptimizeSize == 2 )
1631
+ FuncAttrs.addAttribute (llvm::Attribute::MinSize);
1632
+ }
1633
+
1634
+ if (CodeGenOpts.DisableRedZone )
1635
+ FuncAttrs.addAttribute (llvm::Attribute::NoRedZone);
1636
+ if (CodeGenOpts.NoImplicitFloat )
1637
+ FuncAttrs.addAttribute (llvm::Attribute::NoImplicitFloat);
1638
+
1639
+ if (AttrOnCallSite) {
1640
+ // Attributes that should go on the call site only.
1641
+ if (!CodeGenOpts.SimplifyLibCalls ||
1642
+ CodeGenOpts.isNoBuiltinFunc (Name.data ()))
1643
+ FuncAttrs.addAttribute (llvm::Attribute::NoBuiltin);
1644
+ if (!CodeGenOpts.TrapFuncName .empty ())
1645
+ FuncAttrs.addAttribute (" trap-func-name" , CodeGenOpts.TrapFuncName );
1646
+ } else {
1647
+ // Attributes that should go on the function, but not the call site.
1648
+ if (!CodeGenOpts.DisableFPElim ) {
1649
+ FuncAttrs.addAttribute (" no-frame-pointer-elim" , " false" );
1650
+ } else if (CodeGenOpts.OmitLeafFramePointer ) {
1651
+ FuncAttrs.addAttribute (" no-frame-pointer-elim" , " false" );
1652
+ FuncAttrs.addAttribute (" no-frame-pointer-elim-non-leaf" );
1653
+ } else {
1654
+ FuncAttrs.addAttribute (" no-frame-pointer-elim" , " true" );
1655
+ FuncAttrs.addAttribute (" no-frame-pointer-elim-non-leaf" );
1656
+ }
1657
+
1658
+ FuncAttrs.addAttribute (" less-precise-fpmad" ,
1659
+ llvm::toStringRef (CodeGenOpts.LessPreciseFPMAD ));
1660
+
1661
+ if (!CodeGenOpts.FPDenormalMode .empty ())
1662
+ FuncAttrs.addAttribute (" denormal-fp-math" , CodeGenOpts.FPDenormalMode );
1663
+
1664
+ FuncAttrs.addAttribute (" no-trapping-math" ,
1665
+ llvm::toStringRef (CodeGenOpts.NoTrappingMath ));
1666
+
1667
+ // TODO: Are these all needed?
1668
+ // unsafe/inf/nan/nsz are handled by instruction-level FastMathFlags.
1669
+ FuncAttrs.addAttribute (" no-infs-fp-math" ,
1670
+ llvm::toStringRef (CodeGenOpts.NoInfsFPMath ));
1671
+ FuncAttrs.addAttribute (" no-nans-fp-math" ,
1672
+ llvm::toStringRef (CodeGenOpts.NoNaNsFPMath ));
1673
+ FuncAttrs.addAttribute (" unsafe-fp-math" ,
1674
+ llvm::toStringRef (CodeGenOpts.UnsafeFPMath ));
1675
+ FuncAttrs.addAttribute (" use-soft-float" ,
1676
+ llvm::toStringRef (CodeGenOpts.SoftFloat ));
1677
+ FuncAttrs.addAttribute (" stack-protector-buffer-size" ,
1678
+ llvm::utostr (CodeGenOpts.SSPBufferSize ));
1679
+ FuncAttrs.addAttribute (" no-signed-zeros-fp-math" ,
1680
+ llvm::toStringRef (CodeGenOpts.NoSignedZeros ));
1681
+ FuncAttrs.addAttribute (
1682
+ " correctly-rounded-divide-sqrt-fp-math" ,
1683
+ llvm::toStringRef (CodeGenOpts.CorrectlyRoundedDivSqrt ));
1684
+
1685
+ // TODO: Reciprocal estimate codegen options should apply to instructions?
1686
+ std::vector<std::string> &Recips = getTarget ().getTargetOpts ().Reciprocals ;
1687
+ if (!Recips.empty ())
1688
+ FuncAttrs.addAttribute (" reciprocal-estimates" ,
1689
+ llvm::join (Recips.begin (), Recips.end (), " ," ));
1690
+
1691
+ if (CodeGenOpts.StackRealignment )
1692
+ FuncAttrs.addAttribute (" stackrealign" );
1693
+ if (CodeGenOpts.Backchain )
1694
+ FuncAttrs.addAttribute (" backchain" );
1695
+ }
1696
+
1697
+ if (getLangOpts ().CUDA && getLangOpts ().CUDAIsDevice ) {
1698
+ // Conservatively, mark all functions and calls in CUDA as convergent
1699
+ // (meaning, they may call an intrinsically convergent op, such as
1700
+ // __syncthreads(), and so can't have certain optimizations applied around
1701
+ // them). LLVM will remove this attribute where it safely can.
1702
+ FuncAttrs.addAttribute (llvm::Attribute::Convergent);
1703
+
1704
+ // Exceptions aren't supported in CUDA device code.
1705
+ FuncAttrs.addAttribute (llvm::Attribute::NoUnwind);
1706
+
1707
+ // Respect -fcuda-flush-denormals-to-zero.
1708
+ if (getLangOpts ().CUDADeviceFlushDenormalsToZero )
1709
+ FuncAttrs.addAttribute (" nvptx-f32ftz" , " true" );
1710
+ }
1711
+ }
1712
+
1713
+ void CodeGenModule::AddDefaultFnAttrs (llvm::Function &F) {
1714
+ llvm::AttrBuilder FuncAttrs;
1715
+ ConstructDefaultFnAttrList (F.getName (),
1716
+ F.hasFnAttribute (llvm::Attribute::OptimizeNone),
1717
+ /* AttrOnCallsite = */ false , FuncAttrs);
1718
+ llvm::AttributeSet AS = llvm::AttributeSet::get (
1719
+ getLLVMContext (), llvm::AttributeSet::FunctionIndex, FuncAttrs);
1720
+ F.addAttributes (llvm::AttributeSet::FunctionIndex, AS);
1721
+ }
1722
+
1623
1723
void CodeGenModule::ConstructAttributeList (
1624
1724
StringRef Name, const CGFunctionInfo &FI, CGCalleeInfo CalleeInfo,
1625
1725
AttributeListType &PAL, unsigned &CallingConv, bool AttrOnCallSite) {
1626
1726
llvm::AttrBuilder FuncAttrs;
1627
1727
llvm::AttrBuilder RetAttrs;
1628
- bool HasOptnone = false ;
1629
1728
1630
1729
CallingConv = FI.getEffectiveCallingConvention ();
1631
-
1632
1730
if (FI.isNoReturn ())
1633
1731
FuncAttrs.addAttribute (llvm::Attribute::NoReturn);
1634
1732
@@ -1639,7 +1737,7 @@ void CodeGenModule::ConstructAttributeList(
1639
1737
1640
1738
const Decl *TargetDecl = CalleeInfo.getCalleeDecl ();
1641
1739
1642
- bool HasAnyX86InterruptAttr = false ;
1740
+ bool HasOptnone = false ;
1643
1741
// FIXME: handle sseregparm someday...
1644
1742
if (TargetDecl) {
1645
1743
if (TargetDecl->hasAttr <ReturnsTwiceAttr>())
@@ -1679,7 +1777,6 @@ void CodeGenModule::ConstructAttributeList(
1679
1777
if (TargetDecl->hasAttr <ReturnsNonNullAttr>())
1680
1778
RetAttrs.addAttribute (llvm::Attribute::NonNull);
1681
1779
1682
- HasAnyX86InterruptAttr = TargetDecl->hasAttr <AnyX86InterruptAttr>();
1683
1780
HasOptnone = TargetDecl->hasAttr <OptimizeNoneAttr>();
1684
1781
if (auto *AllocSize = TargetDecl->getAttr <AllocSizeAttr>()) {
1685
1782
Optional<unsigned > NumElemsParam;
@@ -1691,86 +1788,19 @@ void CodeGenModule::ConstructAttributeList(
1691
1788
}
1692
1789
}
1693
1790
1694
- // OptimizeNoneAttr takes precedence over -Os or -Oz. No warning needed.
1695
- if (!HasOptnone) {
1696
- if (CodeGenOpts.OptimizeSize )
1697
- FuncAttrs.addAttribute (llvm::Attribute::OptimizeForSize);
1698
- if (CodeGenOpts.OptimizeSize == 2 )
1699
- FuncAttrs.addAttribute (llvm::Attribute::MinSize);
1700
- }
1791
+ ConstructDefaultFnAttrList (Name, HasOptnone, AttrOnCallSite, FuncAttrs);
1701
1792
1702
- if (CodeGenOpts.DisableRedZone )
1703
- FuncAttrs.addAttribute (llvm::Attribute::NoRedZone);
1704
- if (CodeGenOpts.NoImplicitFloat )
1705
- FuncAttrs.addAttribute (llvm::Attribute::NoImplicitFloat);
1706
1793
if (CodeGenOpts.EnableSegmentedStacks &&
1707
1794
!(TargetDecl && TargetDecl->hasAttr <NoSplitStackAttr>()))
1708
1795
FuncAttrs.addAttribute (" split-stack" );
1709
1796
1710
- if (AttrOnCallSite) {
1711
- // Attributes that should go on the call site only.
1712
- if (!CodeGenOpts.SimplifyLibCalls ||
1713
- CodeGenOpts.isNoBuiltinFunc (Name.data ()))
1714
- FuncAttrs.addAttribute (llvm::Attribute::NoBuiltin);
1715
- if (!CodeGenOpts.TrapFuncName .empty ())
1716
- FuncAttrs.addAttribute (" trap-func-name" , CodeGenOpts.TrapFuncName );
1717
- } else {
1718
- // Attributes that should go on the function, but not the call site.
1719
- if (!CodeGenOpts.DisableFPElim ) {
1720
- FuncAttrs.addAttribute (" no-frame-pointer-elim" , " false" );
1721
- } else if (CodeGenOpts.OmitLeafFramePointer ) {
1722
- FuncAttrs.addAttribute (" no-frame-pointer-elim" , " false" );
1723
- FuncAttrs.addAttribute (" no-frame-pointer-elim-non-leaf" );
1724
- } else {
1725
- FuncAttrs.addAttribute (" no-frame-pointer-elim" , " true" );
1726
- FuncAttrs.addAttribute (" no-frame-pointer-elim-non-leaf" );
1727
- }
1728
-
1797
+ if (!AttrOnCallSite) {
1729
1798
bool DisableTailCalls =
1730
- CodeGenOpts.DisableTailCalls || HasAnyX86InterruptAttr ||
1731
- (TargetDecl && TargetDecl->hasAttr <DisableTailCallsAttr>());
1732
- FuncAttrs.addAttribute (
1733
- " disable-tail-calls" ,
1734
- llvm::toStringRef (DisableTailCalls));
1735
-
1736
- FuncAttrs.addAttribute (" less-precise-fpmad" ,
1737
- llvm::toStringRef (CodeGenOpts.LessPreciseFPMAD ));
1738
-
1739
- if (!CodeGenOpts.FPDenormalMode .empty ())
1740
- FuncAttrs.addAttribute (" denormal-fp-math" ,
1741
- CodeGenOpts.FPDenormalMode );
1742
-
1743
- FuncAttrs.addAttribute (" no-trapping-math" ,
1744
- llvm::toStringRef (CodeGenOpts.NoTrappingMath ));
1745
-
1746
- // TODO: Are these all needed?
1747
- // unsafe/inf/nan/nsz are handled by instruction-level FastMathFlags.
1748
- FuncAttrs.addAttribute (" no-infs-fp-math" ,
1749
- llvm::toStringRef (CodeGenOpts.NoInfsFPMath ));
1750
- FuncAttrs.addAttribute (" no-nans-fp-math" ,
1751
- llvm::toStringRef (CodeGenOpts.NoNaNsFPMath ));
1752
- FuncAttrs.addAttribute (" unsafe-fp-math" ,
1753
- llvm::toStringRef (CodeGenOpts.UnsafeFPMath ));
1754
- FuncAttrs.addAttribute (" use-soft-float" ,
1755
- llvm::toStringRef (CodeGenOpts.SoftFloat ));
1756
- FuncAttrs.addAttribute (" stack-protector-buffer-size" ,
1757
- llvm::utostr (CodeGenOpts.SSPBufferSize ));
1758
- FuncAttrs.addAttribute (" no-signed-zeros-fp-math" ,
1759
- llvm::toStringRef (CodeGenOpts.NoSignedZeros ));
1760
- FuncAttrs.addAttribute (
1761
- " correctly-rounded-divide-sqrt-fp-math" ,
1762
- llvm::toStringRef (CodeGenOpts.CorrectlyRoundedDivSqrt ));
1763
-
1764
- // TODO: Reciprocal estimate codegen options should apply to instructions?
1765
- std::vector<std::string> &Recips = getTarget ().getTargetOpts ().Reciprocals ;
1766
- if (!Recips.empty ())
1767
- FuncAttrs.addAttribute (" reciprocal-estimates" ,
1768
- llvm::join (Recips.begin (), Recips.end (), " ," ));
1769
-
1770
- if (CodeGenOpts.StackRealignment )
1771
- FuncAttrs.addAttribute (" stackrealign" );
1772
- if (CodeGenOpts.Backchain )
1773
- FuncAttrs.addAttribute (" backchain" );
1799
+ CodeGenOpts.DisableTailCalls ||
1800
+ (TargetDecl && (TargetDecl->hasAttr <DisableTailCallsAttr>() ||
1801
+ TargetDecl->hasAttr <AnyX86InterruptAttr>()));
1802
+ FuncAttrs.addAttribute (" disable-tail-calls" ,
1803
+ llvm::toStringRef (DisableTailCalls));
1774
1804
1775
1805
// Add target-cpu and target-features attributes to functions. If
1776
1806
// we have a decl for the function and it has a target attribute then
@@ -1819,21 +1849,6 @@ void CodeGenModule::ConstructAttributeList(
1819
1849
}
1820
1850
}
1821
1851
1822
- if (getLangOpts ().CUDA && getLangOpts ().CUDAIsDevice ) {
1823
- // Conservatively, mark all functions and calls in CUDA as convergent
1824
- // (meaning, they may call an intrinsically convergent op, such as
1825
- // __syncthreads(), and so can't have certain optimizations applied around
1826
- // them). LLVM will remove this attribute where it safely can.
1827
- FuncAttrs.addAttribute (llvm::Attribute::Convergent);
1828
-
1829
- // Exceptions aren't supported in CUDA device code.
1830
- FuncAttrs.addAttribute (llvm::Attribute::NoUnwind);
1831
-
1832
- // Respect -fcuda-flush-denormals-to-zero.
1833
- if (getLangOpts ().CUDADeviceFlushDenormalsToZero )
1834
- FuncAttrs.addAttribute (" nvptx-f32ftz" , " true" );
1835
- }
1836
-
1837
1852
ClangToLLVMArgMapping IRFunctionArgs (getContext (), FI);
1838
1853
1839
1854
QualType RetTy = FI.getReturnType ();
0 commit comments