Skip to content

Internal IR for compiler args #14748

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

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

dcbaker
Copy link
Member

@dcbaker dcbaker commented Jun 25, 2025

This is set to draft because it's very much a Work in Progress.

The way we handle compiler arguments in Meson is to manipulate raw strings. This works fine when the majority of your compilers are the same, or at least attempt to be compatible. Unfortunately, Meson has to deal with many different toolchains, which often have incompatible argument syntax.

This manifests in a number of different ways:

  • It makes de-duplicating or removing arguments difficult. Consider the high complexity of the CompileArgs class
  • It makes checking for a kind of argument difficult. Ie, we have a check for -Wl,-rlink or -rlink inside of build.py, but that could miss -rlink for many compilers.
  • It means when converting a compiler argument from one language to another, like when proxying linker arguments, we need to determine the format the argument is already in and then attempt to convert.
  • It makes it more difficult to set fields in the XCode and VisualStudio backends, which generally do not expect features to be set by compiler arguments, but by setting specific values. It is very easy to see this in the VS backend code, which has long if blocks converting string command line arguments to XML fields

In order to address this I propose moving to an abstract IR for these arguments. This abstract IR would simplify de-duplicating and cleaning arguments, as well as converting their forms. Each Compiler provides a mechanism to convert arguments from their format into the abstract IR, and a method to convert the abstract IR back into concrete arguments in their expected format. We then manipulate the abstract IR more easily. Consider something like this:

[Define('X'), Opaque('-ffoobar'), Warning('foo'), Warning('foo'), Warning('foo', enable=False), Define('X')]

In this format it's pretty easy to look and see that we have two copies of -DX and two copies of -Wfoo along with -Wno-foo, cancelling both of those out.

Below is a very trivial implementation of this idea, with just enough implemented for GCC or Clang to compile the 1 trivial test (run directly). This also is making use of python features we currently cant rely on, I'll rewrite those later.

dcbaker added 5 commits June 25, 2025 15:27
The goal is to be able to convert arguments from raw arguments that are
passed in via the `c_args` and friends methods, as well as from options
and the environment, into an abstract IR. This IR is then passed around
internally, and the backend is responsible for lowering it into the
format that it wants.

I'm hoping that this will simplify some amount of passing arguments
between languages (like C arguments being passed to non-c compilers, and
linker arguments between languages) as well as simplify the non-ninja
backends which generally use a different mechanism than raw compiler
arguments.
…arguments

These will be used to convert the arguments in many cases.
@dnicolodi
Copy link
Member

FWIW, I like this!

:param enable: If true then enable the warning, otherwise suppress it.
"""

target: str
Copy link
Member

Choose a reason for hiding this comment

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

I think an enable member went missing here.

elif arg.startswith('-U'):
ret.append(arguments.Undefine(arg.removeprefix('-U')))
elif arg.startswith('-Wno-'):
ret.append(arguments.Warning(arg.removeprefix('-Wno-'), False))
Copy link
Member

Choose a reason for hiding this comment

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

Handling of arguments starting with -Wno- is duplicated below.


for arg in args:
match arg:
case arguments.Define(name, value):
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be case arguments.Define(name, None)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants