[llvm-branch-commits] [llvm] 71867ed - [IROutliner] Adding support for swift errors

Andrew Litteken via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Dec 29 23:19:37 PST 2020


Author: Andrew Litteken
Date: 2020-12-30T01:14:55-06:00
New Revision: 71867ed5e6606a93f0c1413f205afe3bb16317fe

URL: https://github.com/llvm/llvm-project/commit/71867ed5e6606a93f0c1413f205afe3bb16317fe
DIFF: https://github.com/llvm/llvm-project/commit/71867ed5e6606a93f0c1413f205afe3bb16317fe.diff

LOG: [IROutliner] Adding support for swift errors

Added: 
    llvm/test/Transforms/IROutliner/outlining-swift-error.ll

Modified: 
    llvm/lib/Transforms/IPO/IROutliner.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/IPO/IROutliner.cpp b/llvm/lib/Transforms/IPO/IROutliner.cpp
index d972908f507d..5289826f4a9a 100644
--- a/llvm/lib/Transforms/IPO/IROutliner.cpp
+++ b/llvm/lib/Transforms/IPO/IROutliner.cpp
@@ -81,6 +81,10 @@ struct OutlinableGroup {
   /// Regions.
   unsigned Cost = 0;
 
+  /// The argument that needs to be marked with the swifterr attribute.  If not
+  /// needed, there is no value.
+  Optional<unsigned> SwiftErrorArgument;
+
   /// For the \ref Regions, we look at every Value.  If it is a constant,
   /// we check whether it is the same in Region.
   ///
@@ -352,6 +356,11 @@ Function *IROutliner::createFunction(Module &M, OutlinableGroup &Group,
       Group.OutlinedFunctionType, GlobalValue::InternalLinkage,
       "outlined_ir_func_" + std::to_string(FunctionNameSuffix), M);
 
+  // Transfer the swifterr attribute to the correct function parameter.
+  if (Group.SwiftErrorArgument.hasValue())
+    Group.OutlinedFunction->addParamAttr(Group.SwiftErrorArgument.getValue(),
+                                         Attribute::SwiftError);
+
   Group.OutlinedFunction->addFnAttr(Attribute::OptimizeForSize);
   Group.OutlinedFunction->addFnAttr(Attribute::MinSize);
 
@@ -570,8 +579,17 @@ findExtractedInputToOverallInputMapping(OutlinableRegion &Region,
     assert(InputOpt.hasValue() && "Global value number not found?");
     Value *Input = InputOpt.getValue();
 
-    if (!Group.InputTypesSet)
+    if (!Group.InputTypesSet) {
       Group.ArgumentTypes.push_back(Input->getType());
+      // If the input value has a swifterr attribute, make sure to mark the
+      // argument in the overall function.
+      if (Input->isSwiftError()) {
+        assert(
+            !Group.SwiftErrorArgument.hasValue() &&
+            "Argument already marked with swifterr for this OutlinableGroup!");
+        Group.SwiftErrorArgument = TypeIndex;
+      }
+    }
 
     // Check if we have a constant. If we do add it to the overall argument
     // number to Constant map for the region, and continue to the next input.
@@ -792,6 +810,12 @@ CallInst *replaceCalledFunction(Module &M, OutlinableRegion &Region) {
   OldCall->eraseFromParent();
   Region.Call = Call;
 
+  // Make sure that the argument in the new function has the SwiftError
+  // argument.
+  if (Group.SwiftErrorArgument.hasValue())
+    Call->addParamAttr(Group.SwiftErrorArgument.getValue(),
+                       Attribute::SwiftError);
+
   return Call;
 }
 

diff  --git a/llvm/test/Transforms/IROutliner/outlining-swift-error.ll b/llvm/test/Transforms/IROutliner/outlining-swift-error.ll
new file mode 100644
index 000000000000..a6012cdeb329
--- /dev/null
+++ b/llvm/test/Transforms/IROutliner/outlining-swift-error.ll
@@ -0,0 +1,47 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -verify -iroutliner -ir-outlining-no-cost < %s | FileCheck %s
+
+%swift.error = type opaque
+
+define void @outlining_swifterror1(%swift.error** swifterror %err) {
+; CHECK-LABEL: @outlining_swifterror1(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[X:%.*]] = alloca i64, align 8
+; CHECK-NEXT:    call void @outlined_ir_func_0(i64 5, i64* [[X]], %swift.error** swifterror [[ERR:%.*]])
+; CHECK-NEXT:    ret void
+;
+entry:
+  %x = alloca i64
+  %0 = mul i64 5, 5
+  %1 = add i64 %0, %0
+  store i64 %1, i64* %x
+  %casted = bitcast i64* %x to %swift.error*
+  store %swift.error* %casted, %swift.error** %err
+  ret void
+}
+
+define void @outlining_swifterror2(%swift.error** swifterror %err) {
+; CHECK-LABEL: @outlining_swifterror2(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[X:%.*]] = alloca i64, align 8
+; CHECK-NEXT:    call void @outlined_ir_func_0(i64 3, i64* [[X]], %swift.error** swifterror [[ERR:%.*]])
+; CHECK-NEXT:    ret void
+;
+entry:
+  %x = alloca i64
+  %0 = mul i64 3, 3
+  %1 = add i64 %0, %0
+  store i64 %1, i64* %x
+  %casted = bitcast i64* %x to %swift.error*
+  store %swift.error* %casted, %swift.error** %err
+  ret void
+}
+
+; CHECK: define internal void @outlined_ir_func_0(i64 [[ARG0:%.*]], i64* [[ARG1:%.*]], %swift.error** swifterror [[ARG2:%.*]])
+; CHECK: entry_to_outline:
+; CHECK-NEXT:  [[TMP0:%.*]] = mul i64 [[ARG0]], [[ARG0]]
+; CHECK-NEXT:  [[TMP1:%.*]] = add i64 [[TMP0]], [[TMP0]]
+; CHECK-NEXT:  store i64 [[TMP1]], i64* [[ARG1]], align 4
+; CHECK-NEXT:  %casted = bitcast i64* [[ARG1]] to %swift.error*
+; CHECK-NEXT:  store %swift.error* %casted, %swift.error** [[ARG2]], align 8
+; CHECK-NEXT:  br label %entry_after_outline.exitStub


        


More information about the llvm-branch-commits mailing list