Skip to content

Commit af31642

Browse files
committed
[Clang] Verify data layout consistency
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 lot 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.
1 parent 7e830f7 commit af31642

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,76 @@ 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+
// FIXME: M68k specifies incorrect wrong int and long alignments in Clang
362+
// and incorrect long long alignment in both LLVM and Clang.
363+
if (Triple.getArch() != llvm::Triple::m68k) {
364+
Check("int", llvm::Type::getIntNTy(Context, Target.IntWidth),
365+
Target.IntAlign);
366+
Check("long", llvm::Type::getIntNTy(Context, Target.LongWidth),
367+
Target.LongAlign);
368+
Check("long long", llvm::Type::getIntNTy(Context, Target.LongLongWidth),
369+
Target.LongLongAlign);
370+
}
371+
// FIXME: There are int128 alignment mismatches on multiple targets.
372+
if (Target.hasInt128Type() && !Target.getTargetOpts().ForceEnableInt128 &&
373+
!Triple.isAMDGPU() && !Triple.isSPIRV() &&
374+
Triple.getArch() != llvm::Triple::ve)
375+
Check("__int128", llvm::Type::getIntNTy(Context, 128), Target.Int128Align);
376+
377+
if (Target.hasFloat16Type())
378+
Check("half", llvm::Type::getFloatingPointTy(Context, *Target.HalfFormat),
379+
Target.HalfAlign);
380+
if (Target.hasBFloat16Type())
381+
Check("bfloat", llvm::Type::getBFloatTy(Context), Target.BFloat16Align);
382+
Check("float", llvm::Type::getFloatingPointTy(Context, *Target.FloatFormat),
383+
Target.FloatAlign);
384+
// FIXME: AIX specifies wrong double alignment in DataLayout
385+
if (!Triple.isOSAIX()) {
386+
Check("double",
387+
llvm::Type::getFloatingPointTy(Context, *Target.DoubleFormat),
388+
Target.DoubleAlign);
389+
Check("long double",
390+
llvm::Type::getFloatingPointTy(Context, *Target.LongDoubleFormat),
391+
Target.LongDoubleAlign);
392+
}
393+
// FIXME: Wasm has a mismatch in f128 alignment between Clang and LLVM.
394+
if (Target.hasFloat128Type() && !Triple.isWasm())
395+
Check("__float128", llvm::Type::getFP128Ty(Context), Target.Float128Align);
396+
if (Target.hasIbm128Type())
397+
Check("__ibm128", llvm::Type::getPPC_FP128Ty(Context), Target.Ibm128Align);
398+
399+
// FIXME: Clang specifies incorrect pointer alignment for m68k.
400+
if (Triple.getArch() != llvm::Triple::m68k)
401+
Check("void*", llvm::PointerType::getUnqual(Context), Target.PointerAlign);
402+
#endif
403+
}
404+
335405
CodeGenModule::CodeGenModule(ASTContext &C,
336406
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
337407
const HeaderSearchOptions &HSO,
@@ -487,6 +557,9 @@ CodeGenModule::CodeGenModule(ASTContext &C,
487557

488558
llvm::sort(this->MSHotPatchFunctions);
489559
}
560+
561+
if (!Context.getAuxTargetInfo())
562+
checkDataLayoutConsistency(Context.getTargetInfo(), LLVMContext, LangOpts);
490563
}
491564

492565
CodeGenModule::~CodeGenModule() {}

0 commit comments

Comments
 (0)