Skip to content

Commit b975e13

Browse files
connorjwardinducer
andauthored
Prefer 'static inline' to 'inline' for C functions (#948)
* Prefer 'static inline' to 'inline' for C functions Just passing 'inline' is undefined behaviour and leads to linker failures. See https://stackoverflow.com/questions/16245521/c99-inline-function-in-c-file/16245669#16245669. * Update basedpyright baseline * Revert "Update basedpyright baseline" This reverts commit 0e45e10. * Patch for basedpyright * Add type info for C preamble generators --------- Co-authored-by: Andreas Kloeckner <inform@tiker.net>
1 parent 7e43b8b commit b975e13

File tree

2 files changed

+12
-279
lines changed

2 files changed

+12
-279
lines changed

.basedpyright/baseline.json

Lines changed: 0 additions & 272 deletions
Original file line numberDiff line numberDiff line change
@@ -68875,182 +68875,6 @@
6887568875
}
6887668876
],
6887768877
"./loopy/target/c/__init__.py": [
68878-
{
68879-
"code": "reportUnknownParameterType",
68880-
"range": {
68881-
"startColumn": 27,
68882-
"endColumn": 40,
68883-
"lineCount": 1
68884-
}
68885-
},
68886-
{
68887-
"code": "reportMissingParameterType",
68888-
"range": {
68889-
"startColumn": 27,
68890-
"endColumn": 40,
68891-
"lineCount": 1
68892-
}
68893-
},
68894-
{
68895-
"code": "reportUnknownMemberType",
68896-
"range": {
68897-
"startColumn": 11,
68898-
"endColumn": 28,
68899-
"lineCount": 1
68900-
}
68901-
},
68902-
{
68903-
"code": "reportUnknownArgumentType",
68904-
"range": {
68905-
"startColumn": 11,
68906-
"endColumn": 69,
68907-
"lineCount": 1
68908-
}
68909-
},
68910-
{
68911-
"code": "reportUnknownMemberType",
68912-
"range": {
68913-
"startColumn": 44,
68914-
"endColumn": 69,
68915-
"lineCount": 1
68916-
}
68917-
},
68918-
{
68919-
"code": "reportUnknownMemberType",
68920-
"range": {
68921-
"startColumn": 24,
68922-
"endColumn": 49,
68923-
"lineCount": 1
68924-
}
68925-
},
68926-
{
68927-
"code": "reportUnknownMemberType",
68928-
"range": {
68929-
"startColumn": 11,
68930-
"endColumn": 27,
68931-
"lineCount": 1
68932-
}
68933-
},
68934-
{
68935-
"code": "reportUnknownArgumentType",
68936-
"range": {
68937-
"startColumn": 11,
68938-
"endColumn": 68,
68939-
"lineCount": 1
68940-
}
68941-
},
68942-
{
68943-
"code": "reportUnknownMemberType",
68944-
"range": {
68945-
"startColumn": 43,
68946-
"endColumn": 68,
68947-
"lineCount": 1
68948-
}
68949-
},
68950-
{
68951-
"code": "reportUnknownMemberType",
68952-
"range": {
68953-
"startColumn": 16,
68954-
"endColumn": 43,
68955-
"lineCount": 1
68956-
}
68957-
},
68958-
{
68959-
"code": "reportUnknownMemberType",
68960-
"range": {
68961-
"startColumn": 16,
68962-
"endColumn": 50,
68963-
"lineCount": 1
68964-
}
68965-
},
68966-
{
68967-
"code": "reportUnknownMemberType",
68968-
"range": {
68969-
"startColumn": 16,
68970-
"endColumn": 63,
68971-
"lineCount": 1
68972-
}
68973-
},
68974-
{
68975-
"code": "reportUnknownMemberType",
68976-
"range": {
68977-
"startColumn": 8,
68978-
"endColumn": 41,
68979-
"lineCount": 1
68980-
}
68981-
},
68982-
{
68983-
"code": "reportUnknownParameterType",
68984-
"range": {
68985-
"startColumn": 24,
68986-
"endColumn": 37,
68987-
"lineCount": 1
68988-
}
68989-
},
68990-
{
68991-
"code": "reportMissingParameterType",
68992-
"range": {
68993-
"startColumn": 24,
68994-
"endColumn": 37,
68995-
"lineCount": 1
68996-
}
68997-
},
68998-
{
68999-
"code": "reportMissingParameterType",
69000-
"range": {
69001-
"startColumn": 39,
69002-
"endColumn": 53,
69003-
"lineCount": 1
69004-
}
69005-
},
69006-
{
69007-
"code": "reportUnknownMemberType",
69008-
"range": {
69009-
"startColumn": 15,
69010-
"endColumn": 26,
69011-
"lineCount": 1
69012-
}
69013-
},
69014-
{
69015-
"code": "reportUnknownMemberType",
69016-
"range": {
69017-
"startColumn": 39,
69018-
"endColumn": 67,
69019-
"lineCount": 1
69020-
}
69021-
},
69022-
{
69023-
"code": "reportUnknownMemberType",
69024-
"range": {
69025-
"startColumn": 16,
69026-
"endColumn": 44,
69027-
"lineCount": 1
69028-
}
69029-
},
69030-
{
69031-
"code": "reportUnknownMemberType",
69032-
"range": {
69033-
"startColumn": 11,
69034-
"endColumn": 20,
69035-
"lineCount": 1
69036-
}
69037-
},
69038-
{
69039-
"code": "reportUnknownMemberType",
69040-
"range": {
69041-
"startColumn": 25,
69042-
"endColumn": 45,
69043-
"lineCount": 1
69044-
}
69045-
},
69046-
{
69047-
"code": "reportUnknownMemberType",
69048-
"range": {
69049-
"startColumn": 25,
69050-
"endColumn": 52,
69051-
"lineCount": 1
69052-
}
69053-
},
6905468878
{
6905568879
"code": "reportUnknownMemberType",
6905668880
"range": {
@@ -69059,30 +68883,6 @@
6905968883
"lineCount": 1
6906068884
}
6906168885
},
69062-
{
69063-
"code": "reportUnknownMemberType",
69064-
"range": {
69065-
"startColumn": 20,
69066-
"endColumn": 35,
69067-
"lineCount": 1
69068-
}
69069-
},
69070-
{
69071-
"code": "reportUnknownMemberType",
69072-
"range": {
69073-
"startColumn": 24,
69074-
"endColumn": 44,
69075-
"lineCount": 1
69076-
}
69077-
},
69078-
{
69079-
"code": "reportUnknownMemberType",
69080-
"range": {
69081-
"startColumn": 24,
69082-
"endColumn": 51,
69083-
"lineCount": 1
69084-
}
69085-
},
6908668886
{
6908768887
"code": "reportUnknownMemberType",
6908868888
"range": {
@@ -69091,30 +68891,6 @@
6909168891
"lineCount": 1
6909268892
}
6909368893
},
69094-
{
69095-
"code": "reportUnknownMemberType",
69096-
"range": {
69097-
"startColumn": 20,
69098-
"endColumn": 35,
69099-
"lineCount": 1
69100-
}
69101-
},
69102-
{
69103-
"code": "reportUnknownMemberType",
69104-
"range": {
69105-
"startColumn": 24,
69106-
"endColumn": 44,
69107-
"lineCount": 1
69108-
}
69109-
},
69110-
{
69111-
"code": "reportUnknownMemberType",
69112-
"range": {
69113-
"startColumn": 24,
69114-
"endColumn": 51,
69115-
"lineCount": 1
69116-
}
69117-
},
6911868894
{
6911968895
"code": "reportUnknownMemberType",
6912068896
"range": {
@@ -69123,54 +68899,6 @@
6912368899
"lineCount": 1
6912468900
}
6912568901
},
69126-
{
69127-
"code": "reportUnknownMemberType",
69128-
"range": {
69129-
"startColumn": 20,
69130-
"endColumn": 38,
69131-
"lineCount": 1
69132-
}
69133-
},
69134-
{
69135-
"code": "reportUnknownMemberType",
69136-
"range": {
69137-
"startColumn": 15,
69138-
"endColumn": 30,
69139-
"lineCount": 1
69140-
}
69141-
},
69142-
{
69143-
"code": "reportUnknownMemberType",
69144-
"range": {
69145-
"startColumn": 15,
69146-
"endColumn": 45,
69147-
"lineCount": 1
69148-
}
69149-
},
69150-
{
69151-
"code": "reportUnknownMemberType",
69152-
"range": {
69153-
"startColumn": 15,
69154-
"endColumn": 50,
69155-
"lineCount": 1
69156-
}
69157-
},
69158-
{
69159-
"code": "reportUnknownMemberType",
69160-
"range": {
69161-
"startColumn": 25,
69162-
"endColumn": 36,
69163-
"lineCount": 1
69164-
}
69165-
},
69166-
{
69167-
"code": "reportUnknownMemberType",
69168-
"range": {
69169-
"startColumn": 32,
69170-
"endColumn": 43,
69171-
"lineCount": 1
69172-
}
69173-
},
6917468902
{
6917568903
"code": "reportUnknownArgumentType",
6917668904
"range": {

loopy/target/c/__init__.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@
7575

7676

7777
if TYPE_CHECKING:
78-
from collections.abc import Callable, Mapping, Sequence
78+
from collections.abc import Callable, Iterator, Mapping, Sequence
7979

8080
from numpy.typing import DTypeLike
8181

82-
from loopy.codegen import CodeGenerationState
82+
from loopy.codegen import CodeGenerationState, PreambleInfo
8383
from loopy.codegen.result import CodeGenerationResult
8484
from loopy.kernel import LoopKernel
8585
from loopy.kernel.instruction import MultiAssignmentBase
@@ -209,7 +209,7 @@ def map_nan(self, expr: p.NaN):
209209
return super().map_nan(expr)
210210

211211

212-
def c99_preamble_generator(preamble_info):
212+
def c99_preamble_generator(preamble_info: PreambleInfo) -> Iterator[tuple[str, str]]:
213213
if any(dtype.is_integral() for dtype in preamble_info.seen_dtypes):
214214
yield ("10_stdint", """
215215
#include <stdint.h>
@@ -235,7 +235,12 @@ def c99_preamble_generator(preamble_info):
235235
# }}}
236236

237237

238-
def _preamble_generator(preamble_info, func_qualifier="inline"):
238+
def _preamble_generator(
239+
preamble_info: PreambleInfo,
240+
func_qualifier: str = "static inline"
241+
) -> Iterator[tuple[str, str]]:
242+
assert isinstance(preamble_info.kernel.target, CFamilyTarget)
243+
239244
integer_type_names = ["int8", "int16", "int32", "int64"]
240245

241246
def_integer_types_macro = ("03_def_integer_types", r"""
@@ -333,7 +338,7 @@ def _preamble_generator(preamble_info, func_qualifier="inline"):
333338
}""")
334339

335340
yield (f"07_{func.c_name}", f"""
336-
inline {res_ctype} {func.c_name}({base_ctype} x, {exp_ctype} n) {{
341+
static inline {res_ctype} {func.c_name}({base_ctype} x, {exp_ctype} n) {{
337342
if (n == 0)
338343
return 1;
339344
{re.sub(r"^", 14*" ", signed_exponent_preamble, flags=re.M)}
@@ -778,7 +783,7 @@ def generate_preambles(self, target):
778783
dtype = self.arg_id_to_dtype[0]
779784
ctype = target.dtype_to_typename(dtype)
780785
yield (f"08_c_{self.name_in_target}", f"""
781-
inline static int {self.name_in_target}({ctype} x) {{
786+
static inline static int {self.name_in_target}({ctype} x) {{
782787
return 0;
783788
}}""")
784789

@@ -877,7 +882,7 @@ class CFamilyASTBuilder(ASTBuilderBase[Generable]):
877882

878883
target: CFamilyTarget
879884

880-
preamble_function_qualifier: ClassVar[str] = "inline"
885+
preamble_function_qualifier: ClassVar[str] = "static inline"
881886

882887
# {{{ library
883888

0 commit comments

Comments
 (0)