Skip to content

Commit 5fa4eb1

Browse files
authored
[Clang] Verify data layout consistency (llvm#144720)
Verify that the alignments specified by clang TargetInfo match the alignments specified by LLVM data layout, which will hopefully prevent accidental mismatches in the future. This currently contains opt-outs for a number of of existing mismatches. I'm also skipping the verification if options like `-malign-double` are used, or a language that mandates sizes/alignments that differ from C. The verification happens in CodeGen, as we can't have an IR dependency in Basic.
1 parent cb80651 commit 5fa4eb1

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,72 @@ const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
332332
return *TheTargetCodeGenInfo;
333333
}
334334

335+
static void checkDataLayoutConsistency(const TargetInfo &Target,
336+
llvm::LLVMContext &Context,
337+
const LangOptions &Opts) {
338+
#ifndef NDEBUG
339+
// Don't verify non-standard ABI configurations.
340+
if (Opts.AlignDouble || Opts.OpenCL || Opts.HLSL)
341+
return;
342+
343+
llvm::Triple Triple = Target.getTriple();
344+
llvm::DataLayout DL(Target.getDataLayoutString());
345+
auto Check = [&](const char *Name, llvm::Type *Ty, unsigned Alignment) {
346+
llvm::Align DLAlign = DL.getABITypeAlign(Ty);
347+
llvm::Align ClangAlign(Alignment / 8);
348+
if (DLAlign != ClangAlign) {
349+
llvm::errs() << "For target " << Triple.str() << " type " << Name
350+
<< " mapping to " << *Ty << " has data layout alignment "
351+
<< DLAlign.value() << " while clang specifies "
352+
<< ClangAlign.value() << "\n";
353+
abort();
354+
}
355+
};
356+
357+
Check("bool", llvm::Type::getIntNTy(Context, Target.BoolWidth),
358+
Target.BoolAlign);
359+
Check("short", llvm::Type::getIntNTy(Context, Target.ShortWidth),
360+
Target.ShortAlign);
361+
Check("int", llvm::Type::getIntNTy(Context, Target.IntWidth),
362+
Target.IntAlign);
363+
Check("long", llvm::Type::getIntNTy(Context, Target.LongWidth),
364+
Target.LongAlign);
365+
// FIXME: M68k specifies incorrect long long alignment in both LLVM and Clang.
366+
if (Triple.getArch() != llvm::Triple::m68k)
367+
Check("long long", llvm::Type::getIntNTy(Context, Target.LongLongWidth),
368+
Target.LongLongAlign);
369+
// FIXME: There are int128 alignment mismatches on multiple targets.
370+
if (Target.hasInt128Type() && !Target.getTargetOpts().ForceEnableInt128 &&
371+
!Triple.isAMDGPU() && !Triple.isSPIRV() &&
372+
Triple.getArch() != llvm::Triple::ve)
373+
Check("__int128", llvm::Type::getIntNTy(Context, 128), Target.Int128Align);
374+
375+
if (Target.hasFloat16Type())
376+
Check("half", llvm::Type::getFloatingPointTy(Context, *Target.HalfFormat),
377+
Target.HalfAlign);
378+
if (Target.hasBFloat16Type())
379+
Check("bfloat", llvm::Type::getBFloatTy(Context), Target.BFloat16Align);
380+
Check("float", llvm::Type::getFloatingPointTy(Context, *Target.FloatFormat),
381+
Target.FloatAlign);
382+
// FIXME: AIX specifies wrong double alignment in DataLayout
383+
if (!Triple.isOSAIX()) {
384+
Check("double",
385+
llvm::Type::getFloatingPointTy(Context, *Target.DoubleFormat),
386+
Target.DoubleAlign);
387+
Check("long double",
388+
llvm::Type::getFloatingPointTy(Context, *Target.LongDoubleFormat),
389+
Target.LongDoubleAlign);
390+
}
391+
// FIXME: Wasm has a mismatch in f128 alignment between Clang and LLVM.
392+
if (Target.hasFloat128Type() && !Triple.isWasm())
393+
Check("__float128", llvm::Type::getFP128Ty(Context), Target.Float128Align);
394+
if (Target.hasIbm128Type())
395+
Check("__ibm128", llvm::Type::getPPC_FP128Ty(Context), Target.Ibm128Align);
396+
397+
Check("void*", llvm::PointerType::getUnqual(Context), Target.PointerAlign);
398+
#endif
399+
}
400+
335401
CodeGenModule::CodeGenModule(ASTContext &C,
336402
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
337403
const HeaderSearchOptions &HSO,
@@ -487,6 +553,9 @@ CodeGenModule::CodeGenModule(ASTContext &C,
487553

488554
llvm::sort(this->MSHotPatchFunctions);
489555
}
556+
557+
if (!Context.getAuxTargetInfo())
558+
checkDataLayoutConsistency(Context.getTargetInfo(), LLVMContext, LangOpts);
490559
}
491560

492561
CodeGenModule::~CodeGenModule() {}

0 commit comments

Comments
 (0)