Skip to content

TypeLowering: assume that C unions can contain a pointer #78221

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 1 commit into from
Dec 17, 2024
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
2 changes: 2 additions & 0 deletions include/swift/SIL/TypeLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ class TypeLowering {
}

void setNonTrivial() { Flags |= NonTrivialFlag; }
void setIsOrContainsRawPointer() { Flags |= HasRawPointerFlag; }

void setNonFixedABI() { Flags |= NonFixedABIFlag; }
void setAddressOnly() { Flags |= AddressOnlyFlag; }
void setTypeExpansionSensitive(
Expand Down
10 changes: 10 additions & 0 deletions lib/SIL/IR/TypeLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "swift/SIL/SILModule.h"
#include "swift/SIL/Test.h"
#include "clang/AST/Type.h"
#include "clang/AST/Decl.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"

Expand Down Expand Up @@ -2508,6 +2509,15 @@ namespace {
properties.setLexical(IsLexical);
}

if (auto *clangDecl = D->getClangDecl()) {
if (auto *recordDecl = dyn_cast<clang::RecordDecl>(clangDecl)) {
// C unions are imported as opaque types. Therefore we have to assume
// that a union contains a pointer.
if (recordDecl->isOrContainsUnion())
properties.setIsOrContainsRawPointer();
}
}

// [is_or_contains_pack_unsubstituted] Visit the fields of the
// unsubstituted type to find pack types which would be substituted away.
for (auto field : D->getStoredProperties()) {
Expand Down
9 changes: 9 additions & 0 deletions test/SILOptimizer/Inputs/include/cunion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

struct S {
int i;
};

union U {
struct S *p;
};

6 changes: 6 additions & 0 deletions test/SILOptimizer/Inputs/include/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ module gizmo {
header "Gizmo.h"
export *
}

module CUnion {
header "cunion.h"
export *
}

42 changes: 42 additions & 0 deletions test/SILOptimizer/dead_store_elim_c.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// RUN: %target-sil-opt %s -dead-store-elimination -I %S/Inputs/include | %FileCheck %s

sil_stage canonical

import Builtin
import Swift
import SwiftShims
import CUnion


sil [noinline] @modify_U : $@convention(thin) (@inout U) -> () {
[%0: read v**]
[global: read,write,copy,destroy,allocate,deinit_barrier]
}

// CHECK-LABEL: sil @pointer_escape_via_c_union :
// CHECK: store %0 to %1
// CHECK: } // end sil function 'pointer_escape_via_c_union'
sil @pointer_escape_via_c_union : $@convention(thin) (S) -> () {
[global: read,write,copy,destroy,allocate,deinit_barrier]
bb0(%0 : $S):
%1 = alloc_stack [var_decl] $S, var, name "vs"
store %0 to %1
%3 = address_to_pointer [stack_protection] %1 to $Builtin.RawPointer
%4 = struct $UnsafeMutablePointer<S> (%3)
%5 = alloc_stack [var_decl] $U, var, name "u"
%6 = enum $Optional<UnsafeMutablePointer<S>>, #Optional.some!enumelt, %4
%7 = alloc_stack [var_decl] $U
%8 = address_to_pointer %7 to $Builtin.RawPointer
%9 = pointer_to_address %8 to [strict] $*Optional<UnsafeMutablePointer<S>>
store %6 to %9
%11 = load %7
dealloc_stack %7
store %11 to %5
%14 = function_ref @modify_U : $@convention(thin) (@inout U) -> ()
%15 = apply %14(%5) : $@convention(thin) (@inout U) -> ()
dealloc_stack %5
dealloc_stack %1
%18 = tuple ()
return %18
}