Skip to content

Commit

Permalink
MSVC doesn't support adding alignment to functions, so create a separ…
Browse files Browse the repository at this point in the history
…ate #define for this, which is a no-op on MSVC

Given that this occurs in several tests, and the MSVC tests have error-on-warning, this is a comment rather than a #warning
  • Loading branch information
bcoppens committed Jan 13, 2024
1 parent 8c4c719 commit 0f410a3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
20 changes: 13 additions & 7 deletions lib/Target/CBackend/CBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2051,6 +2051,14 @@ static void defineAligns(raw_ostream &Out) {
Out << "#endif\n\n";
}

static void defineFunctionAlign(raw_ostream &Out) {
Out << "#ifdef _MSC_VER\n";
Out << "#define __FUNCTIONALIGN__(X) /* WARNING: THIS FEATURE IS NOT SUPPORTED BY MSVC! */ \n";
Out << "#else\n";
Out << "#define __FUNCTIONALIGN__(X) __attribute__((aligned(X)))\n";
Out << "#endif\n\n";
}

static void defineUnreachable(raw_ostream &Out) {
Out << "#ifdef _MSC_VER\n";
Out << "#define __builtin_unreachable() __assume(0)\n";
Expand Down Expand Up @@ -2340,6 +2348,8 @@ void CWriter::generateCompilerSpecificCode(raw_ostream &Out,
defineUnalignedLoad(Out);
if (headerIncAligns())
defineAligns(Out);
if (headerIncFunctionAlign())
defineFunctionAlign(Out);
if (headerIncNanInf())
defineNanInf(Out);
if (headerIncInt128())
Expand Down Expand Up @@ -2610,12 +2620,6 @@ void CWriter::generateHeader(Module &M) {
Out << "__MSVC_INLINE__ ";
}

unsigned Alignment = I->getAlignment();
if (Alignment != 0) {
headerUseAligns();
Out << "__PREFIXALIGN__(" << Alignment << ") ";
}

printFunctionProto(Out, &*I, GetValueName(&*I));
printFunctionAttributes(Out, I->getAttributes());
if (I->hasWeakLinkage() || I->hasLinkOnceLinkage()) {
Expand All @@ -2635,8 +2639,10 @@ void CWriter::generateHeader(Module &M) {
Out << " __HIDDEN__";
}

unsigned Alignment = I->getAlignment();
if (Alignment != 0) {
Out << " __POSTFIXALIGN__(" << Alignment << ")";
headerUseFunctionAlign();
Out << " __FUNCTIONALIGN__(" << Alignment << ") ";
}

if (I->hasName() && I->getName()[0] == 1)
Expand Down
4 changes: 4 additions & 0 deletions lib/Target/CBackend/CBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class CWriter : public FunctionPass, public InstVisitor<CWriter> {
bool AttributeList : 1;
bool UnalignedLoad : 1;
bool Aligns : 1;
bool FunctionAlign : 1;
bool NanInf : 1;
bool Int128 : 1;
bool ThreadFence : 1;
Expand All @@ -121,6 +122,7 @@ class CWriter : public FunctionPass, public InstVisitor<CWriter> {
bool ConstantFP80Ty : 1;
bool ConstantFP128Ty : 1;
bool ForceInline : 1;
bool Trap : 1;
} UsedHeaders;

#define USED_HEADERS_FLAG(Name) \
Expand All @@ -146,6 +148,7 @@ class CWriter : public FunctionPass, public InstVisitor<CWriter> {
USED_HEADERS_FLAG(AttributeList)
USED_HEADERS_FLAG(UnalignedLoad)
USED_HEADERS_FLAG(Aligns)
USED_HEADERS_FLAG(FunctionAlign)
USED_HEADERS_FLAG(NanInf)
USED_HEADERS_FLAG(Int128)
USED_HEADERS_FLAG(ThreadFence)
Expand All @@ -155,6 +158,7 @@ class CWriter : public FunctionPass, public InstVisitor<CWriter> {
USED_HEADERS_FLAG(ConstantFP80Ty)
USED_HEADERS_FLAG(ConstantFP128Ty)
USED_HEADERS_FLAG(ForceInline)
USED_HEADERS_FLAG(Trap)

llvm::SmallSet<CmpInst::Predicate, 26> FCmpOps;
void headerUseFCmpOp(CmpInst::Predicate P);
Expand Down
11 changes: 11 additions & 0 deletions test/cpp_tests/test_virtual_function_calls.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#ifndef _MSC_VER
struct A {
int a;
int b;
Expand All @@ -17,3 +18,13 @@ int main() {
return 0;
return 6;
}

#else

/* MSVC doesn't support adding alignment to functions, treat it as an expected failure */

int main() {
return 25;
}

#endif

0 comments on commit 0f410a3

Please sign in to comment.