Skip to content

[6.0] LargeTypesReg2Mem: Add a heuristic for C character arrays #72708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions lib/IRGen/LoadableByAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//===----------------------------------------------------------------------===//

#define DEBUG_TYPE "loadable-address"
#include "Explosion.h"
#include "FixedTypeInfo.h"
#include "IRGenMangler.h"
#include "IRGenModule.h"
Expand Down Expand Up @@ -3431,6 +3432,29 @@ class AddressAssignment {
toDeleteBlockArg.push_back(std::make_pair(b, argIdx));
}

bool isPotentiallyCArray(SILType ty) {
if (ty.isAddress() || ty.isClassOrClassMetatype()) {
return false;
}

auto canType = ty.getASTType();
if (canType->hasTypeParameter()) {
assert(genEnv && "Expected a GenericEnv");
canType = genEnv->mapTypeIntoContext(canType)->getCanonicalType();
}

if (canType.getAnyGeneric() || isa<TupleType>(canType)) {
assert(ty.isObject() &&
"Expected only two categories: address and object");
assert(!canType->hasTypeParameter());
const TypeInfo &TI = irgenModule->getTypeInfoForLowered(canType);
auto explosionSchema = TI.getSchema();
if (explosionSchema.size() > 15)
return true;
}
return false;
}

bool isLargeLoadableType(SILType ty) {
if (ty.isAddress() || ty.isClassOrClassMetatype()) {
return false;
Expand All @@ -3448,7 +3472,11 @@ class AddressAssignment {
assert(!canType->hasTypeParameter());
const TypeInfo &TI = irgenModule->getTypeInfoForLowered(canType);
auto &nativeSchemaOrigParam = TI.nativeParameterValueSchema(*irgenModule);
return nativeSchemaOrigParam.size() > 15;
if (nativeSchemaOrigParam.size() > 15)
return true;
auto explosionSchema = TI.getSchema();
if (explosionSchema.size() > 15)
return true;
}
return false;
}
Expand Down Expand Up @@ -3764,7 +3792,7 @@ class AssignAddressToDef : SILInstructionVisitor<AssignAddressToDef> {
builder.createStore(bc->getLoc(), bc->getOperand(), opdAddr,
StoreOwnershipQualifier::Unqualified);
assignment.mapValueToAddress(origValue, addr);

assignment.markForDeletion(bc);
return;
}
auto opdAddr = assignment.getAddressForValue(bc->getOperand());
Expand Down Expand Up @@ -3926,7 +3954,9 @@ class RewriteUser : SILInstructionVisitor<RewriteUser> {
}

void visitDebugValueInst(DebugValueInst *dbg) {
if (!dbg->hasAddrVal() && overlapsWithOnStackDebugLoc(dbg->getOperand())) {
if (!dbg->hasAddrVal() &&
(assignment.isPotentiallyCArray(dbg->getOperand()->getType()) ||
overlapsWithOnStackDebugLoc(dbg->getOperand()))) {
assignment.markForDeletion(dbg);
return;
}
Expand Down
13 changes: 13 additions & 0 deletions test/IRGen/Inputs/large_union.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

typedef union {
struct {
unsigned char a;
unsigned char arr[32];
} in;
struct {
int a;
unsigned char arr[32];
unsigned char b;
} out;
} some_struct;
22 changes: 22 additions & 0 deletions test/IRGen/large_union.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %target-swift-frontend %s -Osize -Xllvm -sil-print-after=loadable-address -import-objc-header %S/Inputs/large_union.h -c -o %t/t.o 2>&1 | %FileCheck %s

public func test1(_ s: some_struct) -> some_struct {
var copy = s
copy.out.a = 1
return copy
}
// CHECK: sil @$s1t5test1ySo11some_structaADF : $@convention(thin) (@in_guaranteed some_struct) -> @out some_struct {
// CHECK-NOT: unchecked_trivial_bitcast
// CHECK: unchecked_addr_cast {{.*}} : $*some_struct.__Unnamed_struct_out to $*some_struct
// CHECK-NOT: unchecked_trivial_bitcast
// CHECK: } // end sil function '$s1t5test1ySo11some_structaADF'

// CHECK: sil @$s1t5test2yySo11some_structazF : $@convention(thin) (@inout some_struct) -> () {
// CHECK-NOT: unchecked_trivial_bitcast
// CHECK: unchecked_addr_cast {{.*}} : $*some_struct.__Unnamed_struct_out to $*some_struct
// CHECK-NOT: unchecked_trivial_bitcast
// CHECK: } // end sil function '$s1t5test2yySo11some_structazF'

public func test2(_ s: inout some_struct) {
s.out.a = 1
}