[PATCH] [PATCH] Teach the Inliner about attribute optnone

Andrea Di Biagio Andrea_DiBiagio at sn.scee.net
Tue Sep 10 08:48:32 PDT 2013


andreadb added you to the CC list for the revision "[PATCH] Teach the Inliner about attribute optnone".

This patch teaches the Inliner about attribute optnone.

The rules are:
 * a function marked optnone is never inlined;
 * only functions marked always_inline can be inlined inside a function with attribute optnone.

I added an LLVM test that verifies on a simple example the inliner behavior with the presence of an always_inline function, an optnone function and a "normal" function.

Attribute optnone was originally discussed here: http://llvm.1065342.n5.nabble.com/RFC-add-Function-Attribute-to-disable-optimization-td58549.html

r189101 introduced attribute optnone as a function attribute.
I plan to send more patches (two at least) to teach the rest of the optimizer about attribute optnone and also teach clang about this new attribute.

Please let me know what do you think.

Thanks,
Andrea Di Biagio

http://llvm-reviews.chandlerc.com/D1642

Files:
  lib/Analysis/IPA/InlineCost.cpp
  test/Transforms/Inline/inline-optnone.ll

Index: lib/Analysis/IPA/InlineCost.cpp
===================================================================
--- lib/Analysis/IPA/InlineCost.cpp
+++ lib/Analysis/IPA/InlineCost.cpp
@@ -1201,6 +1201,16 @@
     return llvm::InlineCost::getNever();
   }
 
+  // Never inline functions without always-inline attributes if the
+  // caller has optnone attribute.
+  if (CS.getCaller()->hasFnAttribute(Attribute::OptimizeNone))
+    return llvm::InlineCost::getNever();
+
+  // Don't inline this call if the caller has the optnone attribute
+  // (and the callee doesn't have always_inline, checked previously).
+  if (Callee->hasFnAttribute(Attribute::OptimizeNone))
+    return llvm::InlineCost::getNever();
+
   // Never inline functions with conflicting attributes (unless callee has
   // always-inline attribute).
   if (!functionsHaveCompatibleAttributes(CS.getCaller(), Callee))
Index: test/Transforms/Inline/inline-optnone.ll
===================================================================
--- test/Transforms/Inline/inline-optnone.ll
+++ test/Transforms/Inline/inline-optnone.ll
@@ -0,0 +1,52 @@
+; RUN: opt < %s -inline -S | FileCheck %s
+
+; Test that functions with attribute optnone are not inlined.
+; Also test that only functions with attribute alwaysinline
+; are valid candidate for inlining if the caller has optnone attribute.
+
+; Function Attrs: alwaysinline nounwind readnone uwtable
+define i32 @alwaysInlineFunction(i32 %a) #0 {
+entry:
+  %mul = mul i32 %a, %a
+  ret i32 %mul
+}
+
+; Function Attrs: nounwind readnone uwtable
+define i32 @simpleFunction(i32 %a) #1 {
+entry:
+  %add = add i32 %a, %a
+  ret i32 %add
+}
+
+; Function Attrs: nounwind optnone readnone uwtable
+define i32 @OptnoneFunction(i32 %a) #2 {
+entry:
+  %0 = tail call i32 @alwaysInlineFunction(i32 %a)
+  %1 = tail call i32 @simpleFunction(i32 %a)
+  %add = add i32 %0, %1
+  ret i32 %add
+}
+
+; CHECK-LABEL: @OptnoneFunction
+; CHECK-NOT: call i32 @alwaysInlineFunction(i32 %a)
+; CHECK: call i32 @simpleFunction(i32 %a)
+; CHECK: ret
+
+; Function Attrs: nounwind readnone uwtable
+define i32 @bar(i32 %a) #1 {
+entry:
+  %0 = tail call i32 @OptnoneFunction(i32 5)
+  %1 = tail call i32 @simpleFunction(i32 6)
+  %add = add i32 %0, %1
+  ret i32 %add
+}
+
+; CHECK-LABEL: @bar
+; CHECK: call i32 @OptnoneFunction(i32 5)
+; CHECK-NOT: call i32 @simpleFunction(i32 6)
+; CHECK: ret
+
+
+attributes #0 = { alwaysinline nounwind readnone uwtable }
+attributes #1 = { nounwind readnone uwtable }
+attributes #2 = { nounwind optnone readnone uwtable }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1642.1.patch
Type: text/x-patch
Size: 2547 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130910/0ee5f8a3/attachment.bin>


More information about the llvm-commits mailing list