@@ -114,23 +114,30 @@ def _handle_relocation(
114
114
) -> _stencils .Hole :
115
115
raise NotImplementedError (type (self ))
116
116
117
+ def _substitute_in_preserve_none_and_add_musttail (self , ll_file : pathlib .Path ):
118
+ all = ll_file .read_text ()
119
+ # Replace tail calls with musttail and correct calling convetion.
120
+ all = all .replace ("tail call preserve_allcc" , "musttail call preserve_nonecc" )
121
+ all = all .replace ("call preserve_allcc" , "musttail call preserve_nonecc" )
122
+ # Swap sentinel calling conv with the real one.
123
+ all = all .replace ("preserve_allcc" , "preserve_nonecc" )
124
+
125
+ ll_file .write_text (all )
126
+ # with open("/home/ken/Documents/GitHub/cpython/hi.txt", "w") as sys.stdout:
127
+ # print(all)
128
+
117
129
async def _compile (
118
130
self , opname : str , c : pathlib .Path , tempdir : pathlib .Path
119
131
) -> _stencils .StencilGroup :
120
132
o = tempdir / f"{ opname } .o"
121
- args = [
133
+ ll = tempdir / f"{ opname } .ll"
134
+ common_args = [
122
135
f"--target={ self .triple } " ,
123
136
"-DPy_BUILD_CORE_MODULE" ,
124
137
"-D_DEBUG" if self .debug else "-DNDEBUG" ,
125
138
f"-D_JIT_OPCODE={ opname } " ,
126
139
"-D_PyJIT_ACTIVE" ,
127
140
"-D_Py_JIT" ,
128
- "-I." ,
129
- f"-I{ CPYTHON / 'Include' } " ,
130
- f"-I{ CPYTHON / 'Include' / 'internal' } " ,
131
- f"-I{ CPYTHON / 'Include' / 'internal' / 'mimalloc' } " ,
132
- f"-I{ CPYTHON / 'Python' } " ,
133
- f"-I{ CPYTHON / 'Tools' / 'jit' } " ,
134
141
"-O3" ,
135
142
"-c" ,
136
143
# Shorten full absolute file paths in the generated code (like the
@@ -151,11 +158,30 @@ async def _compile(
151
158
"-fno-stack-protector" ,
152
159
"-std=c11" ,
153
160
"-o" ,
154
- f"{ o } " ,
161
+ ]
162
+ ll_args = [
163
+ "-I." ,
164
+ f"-I{ CPYTHON / 'Include' } " ,
165
+ f"-I{ CPYTHON / 'Include' / 'internal' } " ,
166
+ f"-I{ CPYTHON / 'Include' / 'internal' / 'mimalloc' } " ,
167
+ f"-I{ CPYTHON / 'Python' } " ,
168
+ f"-I{ CPYTHON / 'Tools' / 'jit' } " ,
169
+ * common_args ,
170
+ f"{ ll } " ,
155
171
f"{ c } " ,
172
+ "-emit-llvm" ,
173
+ "-S" ,
174
+ * self .args ,
175
+ ]
176
+ o_args = [
177
+ * common_args ,
178
+ f"{ o } " ,
179
+ f"{ ll } " ,
156
180
* self .args ,
157
181
]
158
- await _llvm .run ("clang" , args , echo = self .verbose )
182
+ await _llvm .run ("clang" , ll_args , echo = self .verbose )
183
+ self ._substitute_in_preserve_none_and_add_musttail (ll )
184
+ await _llvm .run ("clang" , o_args , echo = self .verbose )
159
185
return await self ._parse (o )
160
186
161
187
async def _build_stencils (self ) -> dict [str , _stencils .StencilGroup ]:
0 commit comments