[PATCH] Disable inlining between sanitized and non-sanitized functions

Evgeniy Stepanov eugenis at google.com
Tue Jun 25 05:38:17 PDT 2013


Hi chandlerc,

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

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D1035?vs=2555&id=2556#toc

Files:
  test/Transforms/Inline/attributes.ll
  lib/Transforms/IPO/Inliner.cpp

Index: test/Transforms/Inline/attributes.ll
===================================================================
--- test/Transforms/Inline/attributes.ll
+++ test/Transforms/Inline/attributes.ll
@@ -0,0 +1,75 @@
+; RUN: opt < %s -inline -S | FileCheck %s
+target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
+
+; Test that different values of sanitizer attribute prevent inlining.
+
+define i32 @test1f(i32 %i) {
+        ret i32 %i
+}
+
+define i32 @test1(i32 %W) {
+        %X = call i32 @test1f(i32 7)
+        %Y = add i32 %X, %W
+        ret i32 %Y
+; CHECK: @test1(
+; CHECK-NEXT: %Y = add i32 7, %W
+; CHECK-NEXT: ret i32 %Y
+}
+
+
+define i32 @test2f(i32 %i) sanitize_address {
+        ret i32 %i
+}
+
+define i32 @test2(i32 %W) sanitize_address {
+        %X = call i32 @test2f(i32 7)
+        %Y = add i32 %X, %W
+        ret i32 %Y
+; CHECK: @test2(
+; CHECK-NEXT: %Y = add i32 7, %W
+; CHECK-NEXT: ret i32 %Y
+}
+
+
+define i32 @test3f(i32 %i) sanitize_address {
+        ret i32 %i
+}
+
+define i32 @test3(i32 %W) {
+        %X = call i32 @test3f(i32 7)
+        %Y = add i32 %X, %W
+        ret i32 %Y
+; CHECK: @test3(
+; CHECK-NEXT: %X = call i32 @test3f
+; CHECK: ret i32 %Y
+}
+
+
+define i32 @test4f(i32 %i) {
+        ret i32 %i
+}
+
+define i32 @test4(i32 %W) sanitize_address {
+        %X = call i32 @test4f(i32 7)
+        %Y = add i32 %X, %W
+        ret i32 %Y
+; CHECK: @test4(
+; CHECK-NEXT: %X = call i32 @test4f
+; CHECK: ret i32 %Y
+}
+
+
+; alwaysinline is stronger
+
+define i32 @test5f(i32 %i) alwaysinline {
+        ret i32 %i
+}
+
+define i32 @test5(i32 %W) sanitize_address {
+        %X = call i32 @test5f(i32 7)
+        %Y = add i32 %X, %W
+        ret i32 %Y
+; CHECK: @test5(
+; CHECK-NEXT: %Y = add i32 7, %W
+; CHECK-NEXT: ret i32 %Y
+}
Index: lib/Transforms/IPO/Inliner.cpp
===================================================================
--- lib/Transforms/IPO/Inliner.cpp
+++ lib/Transforms/IPO/Inliner.cpp
@@ -257,6 +257,22 @@
   return thres;
 }
 
+/// \brief Test that two functions either have or have not the given attribute
+///        at the same time.
+static bool attributeMatches(Function *F1, Function *F2,
+                             Attribute::AttrKind Attr) {
+  return F1->hasFnAttribute(Attr) == F2->hasFnAttribute(Attr);
+}
+
+/// \brief Test that there are no attribute conflicts between Caller and Callee
+///        that prevent inlining.
+static bool functionsHaveCompatibleAttributes(Function *Caller,
+                                              Function *Callee) {
+  return attributeMatches(Caller, Callee, Attribute::SanitizeAddress) &&
+         attributeMatches(Caller, Callee, Attribute::SanitizeMemory) &&
+         attributeMatches(Caller, Callee, Attribute::SanitizeThread);
+}
+
 /// shouldInline - Return true if the inliner should attempt to inline
 /// at the given CallSite.
 bool Inliner::shouldInline(CallSite CS) {
@@ -273,7 +289,14 @@
           << ", Call: " << *CS.getInstruction() << "\n");
     return false;
   }
-  
+
+  if (!functionsHaveCompatibleAttributes(CS.getCaller(),
+                                         CS.getCalledFunction())) {
+    DEBUG(dbgs() << "    NOT Inlining: incompatible attributes"
+                 << ", Call: " << *CS.getInstruction() << "\n");
+    return false;
+  }
+
   Function *Caller = CS.getCaller();
   if (!IC) {
     DEBUG(dbgs() << "    NOT Inlining: cost=" << IC.getCost()
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1035.2.patch
Type: text/x-patch
Size: 3496 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130625/0138c9d1/attachment.bin>


More information about the llvm-commits mailing list