10000 GH-118943: Fix a race condition when generating `jit_stencils.h` by brandtbucher · Pull Request #118957 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-118943: Fix a race condition when generating jit_stencils.h #118957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make the generation of jit_stencils.h atomic
  • Loading branch information
brandtbucher committed May 11, 2024
commit 3578ca6ef92a4e8173f2a26b84be26f1d63bedeb
38 changes: 21 additions & 17 deletions Tools/jit/_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,18 @@ async def _compile(
await _llvm.run("clang", args_o, echo=self.verbose)
return await self._parse(o)

async def _build_stencils(self) -> dict[str, _stencils.StencilGroup]:
async def _build_stencils(
self, work: pathlib.Path
) -> dict[str, _stencils.StencilGroup]:
generated_cases = PYTHON_EXECUTOR_CASES_C_H.read_text()
opnames = sorted(re.findall(r"\n {8}case (\w+): \{\n", generated_cases))
tasks = []
with tempfile.TemporaryDirectory() as tempdir:
work = pathlib.Path(tempdir).resolve()
async with asyncio.TaskGroup() as group:
coro = self._compile("trampoline", TOOLS_JIT / "trampoline.c", work)
tasks.append(group.create_task(coro, name="trampoline"))
for opname in opnames:
coro = self._compile(opname, TOOLS_JIT_TEMPLATE_C, work)
tasks.append(group.create_task(coro, name=opname))
async with asyncio.TaskGroup() as group:
coro = self._compile("trampoline", TOOLS_JIT / "trampoline.c", work)
tasks.append(group.create_task(coro, name="trampoline"))
for opname in opnames:
coro = self._compile(opname, TOOLS_JIT_TEMPLATE_C, work)
tasks.append(group.create_task(coro, name=opname))
return {task.get_name(): task.result() for task in tasks}

def build(
Expand All @@ -211,14 +211,18 @@ def build(
and jit_stencils.read_text().startswith(digest)
):
return
stencil_groups = asyncio.run(self._build_stencils())
with jit_stencils.open("w") as file:
file.write(digest)
if comment:
file.write(f"// {comment}\n\n")
file.write("")
Copy link
Contributor
@hroncok hroncok May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not entirely sure what was the purpose of this line, but it is no longer there. I guess it did nothing, correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. Yeah, I think this was originally intended to write a newline, but it was really doing nothing. But I think we do want to add a newline whether or not there's a comment, so I'll move one of them from the comment line to here.

for line in _writer.dump(stencil_groups):
file.write(f"{line}\n")
with tempfile.TemporaryDirectory() as tempdir:
work = pathlib.Path(tempdir).resolve()
stencil_groups = asyncio.run(self._build_stencils(work))
new_jit_stencils = work / "jit_stencils.h"
with new_jit_stencils.open("w") as file:
file.write(digest)
if comment:
file.write(f"// {comment}\n\n")
file.write("")
for line in _writer.dump(stencil_groups):
file.write(f"{line}\n")
new_jit_stencils.replace(jit_stencils)


class _COFF(
Expand Down
0