Index: llvm/lib/Transforms/IPO/IROutliner.cpp =================================================================== --- llvm/lib/Transforms/IPO/IROutliner.cpp +++ llvm/lib/Transforms/IPO/IROutliner.cpp @@ -58,6 +58,10 @@ /// index in ArgumentTypes is an output argument. unsigned NumAggregateInputs = 0; + /// The argument that needs to be marked with the swifterr attribute. If not + /// needed, there is no value. + Optional SwiftErrorArgument; + /// For the \ref Regions, we look at every Value. If it is a constant, /// we check whether it is the same in Region. /// @@ -277,6 +281,11 @@ 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); @@ -421,15 +430,24 @@ assert(InputOpt.hasValue() && "Global value number not found?"); Value *Input = InputOpt.getValue(); - if (!Group.InputTypesSet) - Group.ArgumentTypes.push_back(Input->getType()); - // It is not a constant, check if it is a sunken alloca. If it is not, // create the mapping from extracted to overall. If it is, create the // mapping of the index to the value. unsigned Found = ArgInputs.count(Input); assert(Found && "Input cannot be found!"); + 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; + } + } + Region.ExtractedArgToAgg.insert(std::make_pair(OriginalIndex, TypeIndex)); Region.AggArgToExtracted.insert(std::make_pair(TypeIndex, OriginalIndex)); OriginalIndex++; @@ -489,7 +507,15 @@ LLVM_DEBUG(dbgs() << "Replace call to " << *Call << " with call to " << *AggFunc << " with same number of arguments\n"); + Call->setCalledFunction(AggFunc); + + // 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; } Index: llvm/test/Transforms/IROutliner/outlining-swift-error.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/IROutliner/outlining-swift-error.ll @@ -0,0 +1,48 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt -S -verify -iroutliner < %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: [[TMP0:%.*]] = mul i64 5, 5 +; CHECK-NEXT: call void @outlined_ir_func_0(i64 [[TMP0]], 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: [[TMP0:%.*]] = mul i64 3, 3 +; CHECK-NEXT: call void @outlined_ir_func_0(i64 [[TMP0]], 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:%.*]] = add i64 [[ARG0]], [[ARG0]] +; CHECK-NEXT: store i64 [[TMP0]], 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