Skip to content

Commit

Permalink
[clang codegen] Delete unnecessary GEP cleanup code. (#90303)
Browse files Browse the repository at this point in the history
There's some code in AggExprEmitter::VisitCXXParenListOrInitListExpr to
try to do early cleanup for GEPs for fields that aren't accessed. But
it's unlikely to actually save significant compile-time, and it's subtly
wrong in cases where EmitLValueForFieldInitialization() doesn't create a
GEP. So just delete the code.

Fixes #88077. Fixes #89547.
  • Loading branch information
efriedma-quic authored May 29, 2024
1 parent c250aeb commit cbf6e93
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
10 changes: 0 additions & 10 deletions clang/lib/CodeGen/CGExprAgg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1789,25 +1789,15 @@ void AggExprEmitter::VisitCXXParenListOrInitListExpr(
// Push a destructor if necessary.
// FIXME: if we have an array of structures, all explicitly
// initialized, we can end up pushing a linear number of cleanups.
bool pushedCleanup = false;
if (QualType::DestructionKind dtorKind
= field->getType().isDestructedType()) {
assert(LV.isSimple());
if (dtorKind) {
CGF.pushDestroyAndDeferDeactivation(NormalAndEHCleanup, LV.getAddress(),
field->getType(),
CGF.getDestroyer(dtorKind), false);
pushedCleanup = true;
}
}

// If the GEP didn't get used because of a dead zero init or something
// else, clean it up for -O0 builds and general tidiness.
if (!pushedCleanup && LV.isSimple())
if (llvm::GetElementPtrInst *GEP =
dyn_cast<llvm::GetElementPtrInst>(LV.emitRawPointer(CGF)))
if (GEP->use_empty())
GEP->eraseFromParent();
}
}

Expand Down
25 changes: 25 additions & 0 deletions clang/test/CodeGenCXX/no-unique-address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,28 @@ struct HasZeroSizedFieldWithNonTrivialInit {
HasZeroSizedFieldWithNonTrivialInit testHasZeroSizedFieldWithNonTrivialInit = {.a = 1};
// CHECK-LABEL: define {{.*}}cxx_global_var_init
// CHECK: call {{.*}}@_ZN14NonTrivialInitC1Ev({{.*}}@testHasZeroSizedFieldWithNonTrivialInit

void *operator new(unsigned long, void *);
template <class Ty>
struct _box {
[[no_unique_address]] Ty _value;
};
// Make sure this doesn't crash.
// CHECK-LABEL: define {{.*}}placement_new_struct
void placement_new_struct() {
struct set_value_t {};

// GH88077
struct _tuple : _box<set_value_t>, _box<int> {};

int _storage[1];
new (_storage) _tuple{};

// GH89547
struct _tuple2 {
_box<set_value_t> a;
};

int _storage2[1];
new (_storage2) _tuple2{};
}

0 comments on commit cbf6e93

Please sign in to comment.