Skip to content

Commit d8b5118

Browse files
JeffBezansonKeno
authored andcommitted
codegen: ensure required attributes get set on all functions (#32772)
It is especially important that we set the stack-alignment attribute on Win32 on cfunction entry.
1 parent b811a06 commit d8b5118

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

src/codegen.cpp

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
// use ELF because RuntimeDyld COFF X86_64 doesn't seem to work (fails to generate function pointers)?
99
#define FORCE_ELF
1010
#endif
11-
#if defined(_OS_WINDOWS_) || defined(_OS_FREEBSD_)
12-
# define JL_DISABLE_FPO
13-
#endif
1411
#if defined(_CPU_X86_)
1512
#define JL_NEED_FLOATTEMP_VAR 1
1613
#endif
@@ -4444,11 +4441,8 @@ static Function* gen_cfun_wrapper(
44444441
Function *cw = Function::Create(functype,
44454442
GlobalVariable::ExternalLinkage,
44464443
funcName.str(), M);
4447-
jl_init_function(cw);
44484444
cw->setAttributes(attributes);
4449-
#ifdef JL_DISABLE_FPO
4450-
cw->addFnAttr("no-frame-pointer-elim", "true");
4451-
#endif
4445+
jl_init_function(cw);
44524446
Function *cw_proto = into ? cw : function_proto(cw);
44534447
// Save the Function object reference
44544448
if (sf) {
@@ -4763,11 +4757,8 @@ static Function* gen_cfun_wrapper(
47634757
funcName << "_gfthunk";
47644758
Function *gf_thunk = Function::Create(returninfo.decl->getFunctionType(),
47654759
GlobalVariable::InternalLinkage, funcName.str(), M);
4766-
jl_init_function(gf_thunk);
47674760
gf_thunk->setAttributes(returninfo.decl->getAttributes());
4768-
#ifdef JL_DISABLE_FPO
4769-
gf_thunk->addFnAttr("no-frame-pointer-elim", "true");
4770-
#endif
4761+
jl_init_function(gf_thunk);
47714762
// build a specsig -> jl_apply_generic converter thunk
47724763
// this builds a method that calls jl_apply_generic (as a closure over a singleton function pointer),
47734764
// but which has the signature of a specsig
@@ -4844,9 +4835,6 @@ static Function* gen_cfun_wrapper(
48444835
GlobalVariable::ExternalLinkage,
48454836
funcName.str(), M);
48464837
jl_init_function(cw_make);
4847-
#ifdef JL_DISABLE_FPO
4848-
cw_make->addFnAttr("no-frame-pointer-elim", "true");
4849-
#endif
48504838
BasicBlock *b0 = BasicBlock::Create(jl_LLVMContext, "top", cw_make);
48514839
IRBuilder<> cwbuilder(b0);
48524840
Function::arg_iterator AI = cw_make->arg_begin();
@@ -5148,9 +5136,6 @@ static Function *gen_invoke_wrapper(jl_method_instance_t *lam, jl_value_t *jlret
51485136
add_return_attr(w, Attribute::NonNull);
51495137
w->addFnAttr(Thunk);
51505138
jl_init_function(w);
5151-
#ifdef JL_DISABLE_FPO
5152-
w->addFnAttr("no-frame-pointer-elim", "true");
5153-
#endif
51545139
Function::arg_iterator AI = w->arg_begin();
51555140
Value *funcArg = &*AI++;
51565141
Value *argArray = &*AI++;
@@ -5588,23 +5573,8 @@ static std::unique_ptr<Module> emit_function(
55885573
}
55895574
declarations->specFunctionObject = strdup(f->getName().str().c_str());
55905575

5591-
#ifdef JL_DISABLE_FPO
5592-
f->addFnAttr("no-frame-pointer-elim", "true");
5593-
#endif
55945576
if (jlrettype == (jl_value_t*)jl_bottom_type)
55955577
f->setDoesNotReturn();
5596-
#if defined(_OS_WINDOWS_) && !defined(_CPU_X86_64_)
5597-
// tell Win32 to realign the stack to the next 16-byte boundary
5598-
// upon entry to any function. This achieves compatibility
5599-
// with both MinGW-GCC (which assumes an 16-byte-aligned stack) and
5600-
// i686 Windows (which uses a 4-byte-aligned stack)
5601-
AttrBuilder *attr = new AttrBuilder();
5602-
attr->addStackAlignmentAttr(16);
5603-
f->addAttributes(AttributeList::FunctionIndex, *attr);
5604-
#endif
5605-
#if defined(_OS_WINDOWS_) && defined(_CPU_X86_64_)
5606-
f->setHasUWTable(); // force NeedsWinEH
5607-
#endif
56085578

56095579
#ifdef USE_POLLY
56105580
if (!jl_has_meta(stmts, polly_sym) || jl_options.polly == JL_OPTIONS_POLLY_OFF) {

src/jitlayers.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
#include "llvm-version.h"
44
#include "platform.h"
55
#include "options.h"
6+
#if defined(_OS_WINDOWS_) || defined(_OS_FREEBSD_)
7+
# define JL_DISABLE_FPO
8+
#endif
9+
610
#include <iostream>
711
#include <sstream>
812

@@ -822,6 +826,23 @@ bool jl_can_finalize_function(StringRef F)
822826
// let the JIT know this function is a WIP
823827
void jl_init_function(Function *F)
824828
{
829+
// set any attributes that *must* be set on all functions
830+
#if defined(_OS_WINDOWS_) && !defined(_CPU_X86_64_)
831+
// tell Win32 to realign the stack to the next 16-byte boundary
832+
// upon entry to any function. This achieves compatibility
833+
// with both MinGW-GCC (which assumes an 16-byte-aligned stack) and
834+
// i686 Windows (which uses a 4-byte-aligned stack)
835+
AttrBuilder attr;
836+
attr.addStackAlignmentAttr(16);
837+
F->addAttributes(AttributeList::FunctionIndex, attr);
838+
#endif
839+
#if defined(_OS_WINDOWS_) && defined(_CPU_X86_64_)
840+
F->setHasUWTable(); // force NeedsWinEH
841+
#endif
842+
#ifdef JL_DISABLE_FPO
843+
F->addFnAttr("no-frame-pointer-elim", "true");
844+
#endif
845+
// record the WIP name
825846
incomplete_fname.insert(F->getName());
826847
}
827848

0 commit comments

Comments
 (0)