While modifying the code, I realized that we do look into statements
even when there are no replacements. This patch adds the necessary
early bail-outs to avoid that.
ipa_param_body_adjustments::modify_call_stmt cannot have the same at
the very beginning because calls can still contain otherwise removed
parameters that need to be removed from the statements too.
gcc/ChangeLog:
2022-11-11 Martin Jambor <mjambor@suse.cz>
* ipa-param-manipulation.cc
(ipa_param_body_adjustments::modify_expression): Bail out early if
there are no replacements.
(ipa_param_body_adjustments::modify_assignment): Likewise.
The previous patch established a way to propagate information about
parameters from callers to callees (even though then the actual
splitting is done in the opposite direction), this patch adds to that
information about size of the parameters that is known to be safe to
dereference in the callers - the information currently does not come
from actual dereferences but only when we pass a reference to a known
declaration, but we can use e.g. dereferences in BBs dominating the
call, for example too, if we decide it is worth it.
References which look like splitting candidates but are not always
dereferenced are - assuming the dereferences are not improbable - not
discarded straight away but only marked as conditionally
dereferenceable. IPA phase then makes sure that they stay candidates
only if all incoming edges have big enough known-to-be-safe size.
gcc/ChangeLog:
2022-11-11 Martin Jambor <mjambor@suse.cz>
* ipa-sra.cc (isra_param_desc): New fields safe_size,
conditionally_dereferenceable and safe_size_set.
(struct gensum_param_desc): New field conditionally_dereferenceable.
(struct isra_param_flow): Updated comment of field unit_size.
(ipa_sra_function_summaries::duplicate): Copy the new fields.
(isra_call_summary::dump): Dump unit_size when representing safe_size.
(dump_gensum_param_descriptor): Dump new flag.
(dump_isra_param_descriptor): Dump new fields.
(isra_analyze_call): Fill unit_size when it represents known safe
size.
(check_gensum_access): Instead of disqualifying pointers which are not
always dereference, mark them as conditionally dereferencable if loads
are frequent enough.
(process_scan_results): Copy the conditionally_dereferenceable flag.
(isra_write_node_summary): Stream new fields, or assert they are not
initialized yet.
(isra_read_node_info): Stream new fields.
(update_safe_size): New function.
(propagate_param_hints_accross_call): Propagate safe_sizes.
(propagate_hints_to_all_callees): New function.
(adjust_parameter_descriptions): Check conditionally_dereferenceable
candidates, rework dumping.
(ipa_sra_analysis): Move most of hint propagation for one node to
propagate_hints_to_all_callees. Add another loop to stabilize within
SCCs and another one to verify.
gcc/testsuite/ChangeLog:
2022-11-11 Martin Jambor <mjambor@suse.cz>
* gcc.dg/ipa/ipa-sra-26.c: New test.
* gcc.dg/ipa/ipa-sra-27.c: Likewise.
* gcc.dg/ipa/ipa-sra-28.c: Likewise.
Fortran descriptors are structures which are often constructed just
for a particular argument of a particular call where it is passed by
reference. When the called function is under compiler's control, it
can be beneficial to split up the descriptor and pass it in individual
parameters. Unfortunately, currently we allow IPA-SRA to replace a
pointer with a set of replacements which are at most twice as big in
total and for descriptors we'd need to bump that factor to seven.
This patch looks for parameters which are ADDR_EXPRs of local
variables which are written to and passed as arguments by reference
but are never loaded from and marks them with a flag in the call edge
summary. The IPA analysis phase then identifies formal parameters
which are always fed such arguments and then is more lenient when it
comoes to size.
In order not to store to maximums per formal parameter, I calculate
the more lenient one by multiplying the existing one with a new
parameter. If it is preferable to keep the maximums independent, we
can do that. Documentation for the new parameter is missing as I
still need to re-base the patch on a version which has sphinx. I will
write it before committing.
I have disable IPA-SRA in pr48636-2.f90 in order to be able to keep
using its dump-scan expressions. The new testcase is basically a copy
of it with different options and IPA-SRA dump scans.
gcc/ChangeLog:
2022-11-11 Martin Jambor <mjambor@suse.cz>
* ipa-sra.cc (isra_param_desc): New field not_specially_constructed.
(struct isra_param_flow): New field constructed_for_calls.
(isra_call_summary::dump): Dump the new flag.
(loaded_decls): New variable.
(dump_isra_param_descriptor): New parameter hints, dump
not_specially_constructed if it is true.
(dump_isra_param_descriptors): New parameter hints, pass it to
dump_isra_param_descriptor.
(ipa_sra_function_summaries::duplicate): Duplicate new flag.
(create_parameter_descriptors): Adjust comment.
(get_gensum_param_desc): Bail out when decl2desc is NULL.
(scan_expr_access): Add loaded local variables to loaded_decls.
(scan_function): Survive if final_bbs is NULL.
(isra_analyze_call): Compute constructed_for_calls flag.
(process_scan_results): Be optimistic about size limits. Do not dump
computed param hints when dumpint IPA-SRA structures.
(isra_write_edge_summary): Stream constructed_for_calls.
(isra_read_edge_summary): Likewise.
(ipa_sra_dump_all_summaries): New parameter hints, pass it to
dump_isra_param_descriptor.
(flip_all_hints_pessimistic): New function.
(flip_all_param_hints_pessimistic): Likewise.
(propagate_param_hints): Likewise.
(disable_unavailable_parameters): Renamed to
adjust_parameter_descriptions. Expand size limits for parameters
which are specially contstructed by all callers. Check limits again.p
(ipa_sra_analysis): Pass required hints to ipa_sra_dump_all_summaries.
Add hint propagation.
(ipa_sra_summarize_function): Initialize and destory loaded_decls,
rearrange so that scan_function is called even when there are no
candidates.
* params.opt (ipa-sra-ptrwrap-growth-factor): New parameter.
gcc/testsuite/ChangeLog:
2021-11-11 Martin Jambor <mjambor@suse.cz>
* gfortran.dg/pr48636-2.f90: Disable IPA-SRA.
* gfortran.dg/ipa-sra-1.f90: New test.
This patch does not do any functional changes, it merely moves
top-down propagation in the IPA-SRA WPA phase before bottom-up one.
This also meant moving some preliminary checks from the latter to the
former - where they need to be in their own loop over each SCC because
the subsequent one looks at callers.
Currently the propagations are independent (top-down is used for
return value rermoval, bottom-up for parameter removal and splitting)
but subsequent patches will introduce flags about parameters which
should be propagated from callers first and used in splitting. I
separated this change to test ir independently and make those
subsequent patches cleaner.
While at it, I also replaced couple of FOR_EACH_VEC_ELT macros with
C++11 style iteration.
gcc/ChangeLog:
2022-11-11 Martin Jambor <mjambor@suse.cz>
* ipa-sra.cc (ipa_sra_analysis): Move top-down analysis before
bottom-up analysis. Replace FOR_EACH_VEC_ELT with C++11 iteration.
gcc/testsuite/ChangeLog:
2021-12-14 Martin Jambor <mjambor@suse.cz>
* gcc.dg/ipa/ipa-sra-25.c: New test
C++ and especially Fortran pass data by references which are not
pointers potentially pointing anywhere and so can be assumed to be
safely dereferencable. This patch teaches IPA-SRA to treat them as
such and avoid the dance we do to prove that we can move loads from
them to the caller.
When we do not know that a dereference will happen all the time, we
need a heuristics so that we do not force memory accesses that normally
happen only rarely. The patch simply uses the (possibly guessed)
profile and checks whether the (expected) number of loads is at least
half of function invocations invocations - the half is now
configurable with a param as requested by Honza.
gcc/ChangeLog:
2022-12-13 Martin Jambor <mjambor@suse.cz>
PR ipa/103585
* params.opt (ipa-sra-deref-prob-threshold): New parameter.
* doc/invoke.texi (ipa-sra-deref-prob-threshold): Document it.
* ipa-sra.cc (struct gensum_param_access): New field load_count.
(struct gensum_param_desc): New field safe_ref, adjusted comments.
(by_ref_count): Renamed to unsafe_by_ref_count, adjusted all uses.
(dump_gensum_access): Dump the new field.
(dump_gensum_param_descriptor): Likewise.
(create_parameter_descriptors): Set safe_ref field, move setting
by_ref forward. Only increment unsafe_by_ref_count for unsafe
by_ref parameters.
(allocate_access): Initialize new field.
(mark_param_dereference): Adjust indentation. Only add data to
bb_dereferences for unsafe by_ref parameters.
(scan_expr_access): For loads, accumulate BB counts.
(dereference_probable_p): New function.
(check_gensum_access): Fix leading comment, add parameter FUN.
Check cumulative counts of loads for safe by_ref accesses instead
of dereferences.
(process_scan_results): Do not propagate dereference distances for
safe by_ref parameters. Pass fun to check_gensum_access. Safe
by_ref params do not need the postdominance check.
gcc/testsuite/ChangeLog:
2022-11-11 Martin Jambor <mjambor@suse.cz>
* g++.dg/ipa/ipa-sra-5.C: New test
Looking at some benchmarks I have noticed many cases when IPA-CP
cloned a function for all contexts just because it knew that some
parameters were not used at all. Then IPA-SRA looked at the function
and cloned it again to split another parameter or two. The latter
pass is better equipped to detect when parameters can be altogether
removed and so the IPA-CP cloning was for no good reason.
This patch simply alters the IPA-CP not to do that in the situations
where IPA-SRA can (for nodes which can be made local) with additional
dumping requested by Honza.
gcc/ChangeLog:
2022-12-13 Martin Jambor <mjambor@suse.cz>
* ipa-cp.cc (clone_for_param_removal_p): New function.
(estimate_local_effects): Call it before considering cloning
just to remove unused parameters.
This is basically a better fix for PR 103227. The one currently in
use, rushed in late at stage3, which means that IPA-CP transformation
simply does a replacement of default-definition of IPA-SRA-created
scalar parameters with a constant, meant that IPA-SRA actually often
led to creation of a bunch of unused parameters, which was rather
ironic and sub-optimal.
This patch rips that old way out and makes sure the clash is resolved
at clone-materialization time. What happens is that:
1) IPA-SRA IPA analysis (decision) stage recognizes the clash and does
not create a param adjustment entry for such a scalar component.
2) Clone materialization code checks the IPA-CP transformation
summary and when it realizes that it is removing a parameter that
is a base for a discovered IPA-CP aggregate constant, and:
a) the value is passed by reference, it internally records that any
load of the value is replaced directly with the known constant
value. IPA-SRA will not attempt to split values passed by
reference when there is a write to it so we know such a load
won't be on a a LHS.
b) the value is passed by value, there can be stores to the
corresponding bit of the aggregate and so all accesses are
replaced with a new decl and an assignment of the constant to
this decl is generated at the beginning of the function.
The new testcase contains an xfail as the patch does not fix PR 107640
but it is one that ICEs when one is not careful about remapping
indices of parameters, so I'd like to have it in testsuite/gcc.gd/ipa/
even now.
I don't think that PR 107640 should be attempted through
ipa-param-manipulation replacements because the information is not
really there any more and we'd either need to do the replacements
earlier or dig deep into the clone parent info. Instead, we should
record somewhere that at the beginning of the function the bits of the
global decl have known values and use that in the value numbering.
That way we could one day encode also known constants in globals that
do not come through parameters.
gcc/ChangeLog:
2022-11-11 Martin Jambor <mjambor@suse.cz>
PR ipa/103227
* ipa-param-manipulation.h (class ipa_param_adjustments): Removed
member function get_updated_index_or_split.
(class ipa_param_body_adjustments): New overload of
register_replacement, new member function append_init_stmts, new
member m_split_agg_csts_inits.
* ipa-param-manipulation.cc: Include ipa-prop.h.
(ipa_param_adjustments::get_updated_index_or_split): Removed.
(ipa_param_body_adjustments::register_replacement): New overload, use
it from the older one.
(ipa_param_body_adjustments::common_initialization): Added the
capability to create replacements for conflicting IPA-CP discovered
constants.
(ipa_param_body_adjustments::ipa_param_body_adjustments): Construct
the new member.
(ipa_param_body_adjustments::append_init_stmts): New function.
* ipa-sra.cc: Include ipa-prop.h.
(push_param_adjustments_for_index): Require IPA-CP transformation
summary as a parameter, do not create replacements which are known to
have constant values.
(process_isra_node_results): Find and pass to the above function the
IPA-CP transformation summary.
* ipa-prop.cc (adjust_agg_replacement_values): Remove the
functionality replacing IPA-SRA created scalar parameters with
constants. Simplify, do not require parameter descriptors, do not
return anything.
(ipcp_transform_function): Simplify now that
adjust_agg_replacement_values does not change cfg. Move definition
and initialization of descriptors lower.
* tree-inline.cc (tree_function_versioning): Call append_init_stmts of
param_body_adjs, if there are any.
gcc/testsuite/ChangeLog:
2022-11-11 Martin Jambor <mjambor@suse.cz>
PR ipa/103227
PR ipa/107640
* gcc.dg/ipa/pr107640-2.c: New test.
IPA-CP transformation summary streaming code currently won't stream
out transformations necessary for clones which are only necessary for
materialization of other clones (such as an IPA-CP clone which is then
cloned again by IPA-SRA). However, a follow-up patch for bettor
reconciling IPA-SRA and IPA-CP modifications requires to have that
information at its disposal and so this one reworks the streaming to
write out all non-empty transformation summaries.
In order not to stream transformation summaies into partitions where
the node itself nor any of its clones are materialized, I had to make
sure that clones also get encode_body flag in the encoder (so that it
could be tested) and therefore in turn lto_output understands it needs
to skip clones.
This should actually mean less streaming in typical case because
previously we streamed three zeros for all nodes in a partition with
no useful information associated with them. Currently we don't stream
anything for those.
When reworking the streaming, I also simplified it a little a
converted it writing to nicer C++ vector iterations.
gcc/ChangeLog:
2022-11-25 Martin Jambor <mjambor@suse.cz>
* ipa-prop.cc (useful_ipcp_transformation_info_p): New function.
(write_ipcp_transformation_info): Added a parameter, simplified
given that is known not to be NULL.
(ipcp_write_transformation_summaries): Write out all useful
transformation summaries.
(read_ipcp_transformation_info): Simplify given that some info
will be read.
(read_replacements_section): Remove assert.
* lto-cgraph.cc (add_node_to): Also set encode_body for clones.
* lto-streamer-out.cc (lto_output): Do not output virtual clones.
The following patch fixes 2 issues with the *concat<half><mode>3_5 and
*concat<mode><dwi>3_{6,7} patterns.
One is that if the destination is memory rather than register, then
we can't use movabsq and so can't support all the possible immediates.
I see 3 possibilities to fix that. One would be to use
x86_64_hilo_int_operand predicate instead of const_scalar_int_operand
and thus not match it at all during combine in such cases, but that
unnecessarily pessimizes also the case when it is loaded into register
where we can use movabsq.
Another one is what is implemented in the patch, use Wd constraint
for the integer on 64-bit if destination is memory and n otherwise.
Yet another option would be to add match_scratch to the pattern and use
it with =X constraints except for the =o case for 64-bit non-Wd where it
would give a single DImode register (rather than 2).
Another thing is that if one half of the constant is
ix86_endbr_immediate_operand, then for -fcf-protection=branch we
force those constants into memory and that might not work properly
with -fpic. So we should refuse to match with such constants.
OT, seems for movabsq we don't check that and happily allow the endbr
pattern in the immediate.
2022-12-13 Jakub Jelinek <jakub@redhat.com>
PR target/108044
* config/i386/i386.md (*concat<half><mode>3_5, *concat<mode><dwi>3_6,
*concat<mode><dwi>3_7): Split alternative with =ro output constraint
into =r,o,o and use Wd input constraint for the last alternative which
is enabled for TARGET_64BIT. Reject ix86_endbr_immediate_operand
in the input constant.
* gcc.target/i386/pr108044-1.c: New test.
* gcc.target/i386/pr108044-2.c: New test.
* gcc.target/i386/pr108044-3.c: New test.
* gcc.target/i386/pr108044-4.c: New test.
When writing the r13-4629 commit log I've realized that libsanitizer
isn't the only place which nowadays renames libbacktrace symbols,
libstdc++ does that too.
2022-12-13 Jakub Jelinek <jakub@redhat.com>
* src/libbacktrace/backtrace-rename.h (backtrace_uncompress_zstd):
Define.
gcc/fortran/ChangeLog:
PR fortran/107423
* parse.cc (parse_spec): Avoid NULL pointer dereference when parsing
a function and an error occured.
gcc/testsuite/ChangeLog:
PR fortran/107423
* gfortran.dg/pr107423.f90: New test.
the baremetal builds are currently broken because the shift ends up in the wrong
representation if the mode is SImode and the shift amount if 31. To fix this
create the rtx constant with an explicit mode so the backend passes know which
representation it needs to take.
gcc/ChangeLog:
* config/aarch64/aarch64.md (tbranch_<code><mode>3): Use gen_int_mode.
Since vect_recog_rotate_pattern has been extended to work also
on signed types in r13-1100 we miscompile the testcase below.
vect_recog_rotate_pattern actually emits correct scalar code into
the pattern def sequence (in particular cast to utype, doing the
2 shifts in utype so that the right shift is logical and not arithmetic,
or and then cast back to the signed type), but it didn't supply vectype
for most of those pattern statements, which means that the generic handling
fills it up later with the vectype provided by vect_recog_rotate_pattern.
The problem is that it is vectype of the result of the whole pattern,
i.e. vector of signed values in this case, while the conversion to utype,
2 shifts and or (everything with utype lhs in scalar code) should have
uvectype as STMT_VINFO_VECTYPE.
2022-12-13 Jakub Jelinek <jakub@redhat.com>
PR tree-optimization/108064
* tree-vect-patterns.cc (vect_recog_rotate_pattern): Pass uvectype
as 4th argument to append_pattern_def_seq for statements with lhs
with utype type.
* gcc.c-torture/execute/pr108064.c: New test.
This makes sure we treat .DEFERRED_INIT as producing UNDEFINED so
we can continue optimizing uninitialized uses the same as without
-ftrivial-auto-var-init=zero. For the testcase this means we
catch the return 1 optimization opportunity at CCP rather than
only at FRE which already does the right thing here.
PR tree-optimization/105801
* tree-ssa-ccp.cc (likely_value): .DEFERRED_INIT produces
UNDEFINED.
* doc/invoke.texi (ftrivial-auto-var-init): Explicitely
mention we treat variables without an initializer as
undefined also for optimization purposes.
* gcc.dg/tree-ssa/ssa-ccp-43.c: New testcase.
We still need to write out a documentation section, but these READMEs will
help in the meantime.
gcc/rust/
* CONTRIBUTING.md: New.
* README.md: New.
* logo.png: New.
This was a copy/paste from gccgo front-end. We do not use any of the
target_libs yet, but we will need these when we support the libpanic crate.
gcc/rust/
* config-lang.in: New.
This commits ports over the flag/env variable checking from #1540 which only
allows compilation using gccrs if a certain flag is specified
gcc/rust/
* lang.opt (-frust-incomplete-and-experimental-compiler-do-not-use):
New.
* rust-session-manager.cc (Session::compile_crate): Check it.
* Make-lang.in (RUST_SELFTEST_FLAGS): Add it.
gcc/testsuite/
* lib/rust.exp (rust_init): Add
'-frust-incomplete-and-experimental-compiler-do-not-use'.
We have some Rust-specific language options. Note that -fwrapv is
enabled by default in the code. We are trying to respect options such as
-Wunused-result which we get by porting over C++'s no-discard for Rust's
must-use attribute, so we have enabled these by default directly here.
gcc/rust/
* lang.opt: New.
This is a wrapper to get out of C land in the rust-lang.cc and into our
class hierarchy for the rust front-end. We expect that the front-end only
support one source file input as the expansion pass will attempt to resolve
that relative pass and parse accordingly.
The main missing piece here is that we are using saw_errors() to return
early which is unnecessary but as our error handling has been improving
over time we will start to take advantage of error node in our type system
as well as error_mark_node from GCC. The caveat being that our lints/checks
expect no errors and will throw an assertion.
gcc/rust/
* rust-lang.cc: New.
* rust-session-manager.cc: New.
* rust-session-manager.h: New.
Our compiler driver is fairly simple so far. The key piece to enforce is
that a compilation unit in Rust is the whole crate, so the process for
compiling Rust means pointing the compiler at the main entry point such as
src/lib.rs or src/main.rs where the expansion pass takes over loading the
other source files to include them in the crate.
gcc/rust/
* rustspec.cc: New.
The wrappers over linemap and location will eventually disappear here but
served as a useful starting point for us. We have wrappers over the
diagnostics system which we might be able to get rid of as well.
gcc/rust/
* rust-diagnostics.cc: New.
* rust-diagnostics.h: New.
* rust-gcc-diagnostics.cc: New.
* rust-linemap.cc: New.
* rust-linemap.h: New.
* rust-location.h: New.
* rust-system.h: New.
This patch contains the entry point and utilities used for the lowering
of HIR nodes to `tree`s. It also contains a constant evaluator, ported
over from the C++ frontend.
gcc/rust/
* backend/rust-compile-context.cc: New.
* backend/rust-compile-context.h: New.
* backend/rust-compile.cc: New.
* backend/rust-compile.h: New.
* backend/rust-constexpr.cc: New.
* backend/rust-constexpr.h: New.
Co-authored-by: David Faust <david.faust@oracle.com>
Co-authored-by: Faisal Abbas <90.abbasfaisal@gmail.com>
Signed-off-by: Faisal Abbas <90.abbasfaisal@gmail.com>
This pass walks the HIR crate and turns them into GCC `tree`s. We do not have
any Rust specific tree's. We are slowly removing the backend abstraction
which was ported over from gccgo in favour of using `tree`s directly.
gcc/rust/
* backend/rust-builtins.h: New.
* backend/rust-compile-base.cc: New.
* backend/rust-compile-base.h: New.
* backend/rust-mangle.cc: New.
* backend/rust-mangle.h: New.
* backend/rust-tree.cc: New.
* backend/rust-tree.h: New.
* rust-backend.h: New.
* rust-gcc.cc: New.
Co-authored-by: David Faust <david.faust@oracle.com>
Extern crates statements to tell the front-end to look for another library.
The mechanism here is heavily inspired from gccgo, so when we compile a
library for example we invoke:
gccrs -g -O2 -frust-crate=mylib -c src/lib.rs -o src/mylib.o
All going well this object file will now contain extra data inside
.rust-export section inside the object file which will be preserved inside
archives and shared objects. When we have another application which uses
this library 'mylib'.
extern crate mylib;
use mylib::foo;
fn main() {
foo();
}
We compile using:
gcc -g -O2 -frust-crate=test -c src/main.rs -o src/main.o
When the extern crate line is hit the front-end will look for mylib.o,
libmylib.a, mylib.rox. If it finds a raw object file it will read the
.rust-export section directly from the object for the public metadata
such as public functions, types constants etc. If it fails to find an
object it might find .rox which is the objdump of the .rust-export to a
raw file, it might even find libmylib.a and read the export directly out
of the archive file reusing code from gccgo to do so.
The full compiler pipeline is reused here, so the metatadata is actually
just real rust code. The benifit here is that Rust supports exporting,
macros and generics so this requires the name-resolution and type info
all to be generated and inserted into the apropriate context classes. Since
the metadata is real rust code it means we can reuse the full pipeline to
generate the code as necessary. So for the simple case of a public struct
we simply emit the AST dump of this struct directly into the metadata. If
its a non-generic public function we emit and extern rust abi block for
that function. If its a trait we can simply emit the trait with the public
memebers. Generics are more complicated since we need to emit the function
fully for it to be compiled correctly this still needs tests to be added.
The hardest part is non generic impl blocks which is still a WIP.
To finally link the two crates together you run:
gcc -g -O2 -o rust-program.exe src/main.o src/mylib.o
gcc/rust/
* metadata/rust-export-metadata.cc: New.
* metadata/rust-export-metadata.h: New.
* metadata/rust-extern-crate.cc: New.
* metadata/rust-extern-crate.h: New.
* metadata/rust-import-archive.cc: New.
* metadata/rust-imports.cc: New.
* metadata/rust-imports.h: New.
* rust-object-export.cc: New.
* rust-object-export.h: New.
This is a simple walk_tree which acts on the monomorphized code by walking
the compiled translation unit of functions.
gcc/rust/
* checks/lints/rust-lint-unused-var.cc: New.
* checks/lints/rust-lint-unused-var.h: New.
In order to find dead code we use a depth first search and keep liveness
variables, after type resolution. In this case, if a function is unused
and it calls another function the 2nd function is now unused since the
caller is not used etc. The algorithm is a depth first search.
gcc/rust/
* checks/lints/rust-lint-marklive-base.h: New.
* checks/lints/rust-lint-marklive.cc: New.
* checks/lints/rust-lint-marklive.h: New.
* checks/lints/rust-lint-scan-deadcode.h: New.
This pass is responsible for resolving the privacy of items and verifying
that access to these items is performed within the limits of that privacy.
By default, items in Rust are private and only public to the current
module and its submodules. However, the user can annotate an item with
various qualifiers such as `pub` to publicly expose an item. Furthermore,
a module path can be given to `pub` to restrict an item's privacy to a
certain module: These paths need to be resolved and later on checked by
the privacy error reporter.
gcc/rust/
* checks/errors/privacy/rust-privacy-check.cc: New.
* checks/errors/privacy/rust-privacy-check.h: New.
* checks/errors/privacy/rust-privacy-common.h: New.
* checks/errors/privacy/rust-privacy-ctx.cc: New.
* checks/errors/privacy/rust-privacy-ctx.h: New.
* checks/errors/privacy/rust-privacy-reporter.cc: New.
* checks/errors/privacy/rust-privacy-reporter.h: New.
* checks/errors/privacy/rust-pub-restricted-visitor.cc: New.
* checks/errors/privacy/rust-pub-restricted-visitor.h: New.
* checks/errors/privacy/rust-reachability.cc: New.
* checks/errors/privacy/rust-reachability.h: New.
* checks/errors/privacy/rust-visibility-resolver.cc: New.
* checks/errors/privacy/rust-visibility-resolver.h: New.
Similarly to the unsafe checker, constant evaluation can only be performed
in a few contexts and include restrictions on the Rust language. Should
the user fail to uphold those conditions, errors will be reported and the
compilation pipeline interrupted.
These contexts are as follow:
- Array type length expressions
- Array repeat length expressions
- Constants
- Statics
- Enum discriminants
- Const generic arguments
In these contexts, the user is restricted to calling only functions marked
as `const` or perform arithmetic operations only on certain types, among
other restrictions.
gcc/rust/
* checks/errors/rust-const-checker.cc: New.
* checks/errors/rust-const-checker.h: New.
The UnsafeChecker visitor verifies that unsafe actions are only performed
in unsafe contexts. Otherwise, an error should be reported to the user and
the compilation pipeline should be halted. These contexts, which include
unsafe blocks or unsafe functions, are allowed to perform more actions
than regular safe Rust code. These actions currently include:
- Dereferencing raw pointers
- Calls to unsafe functions
- Use of inline assembly
- Use of mutable static
- Use of extern static
- Access to a union's field
- Call to functions with #[target(feature)] attribute
- Initializing type with rustc_layout_scalar_valid_range attribute
- Mutation of layout constrained field
- Borrow of layout constrained field
gcc/rust/
* checks/errors/rust-unsafe-checker.cc: New.
* checks/errors/rust-unsafe-checker.h: New.
Contains abstractions over Rust's types, used when performing the
HIR's type-resolution.
gcc/rust/
* typecheck/rust-tyty.cc: New.
* typecheck/rust-tyty.h: New.
The attribute checker is responsible for checking the validity of various
attributes including built-in ones. It is currently unfinished and will
receive some modifications, as well as become the host of some existing
code in the compiler which needs to be refactored. One of its
responsibilities is to make sure that arguments given to built-in
attributes are correct, or contain the correct type of information. This
visitor also checks that an attribute is allowed to be used in the current
particular context.
gcc/rust/
* util/rust-attributes.cc: New.
* util/rust-attributes.h: New.
This hash was ported from the Go runtime, as we needed a hash for the legacy
symbol mangling system. All symbols in Rust contain a hash of some
metadata for uniqueness on generic functions.
gcc/rust/
* util/fnv-hash.h: New.
This is a wrapper for make_unique. We can likely get rid of this, as there
are other implementations available, or simply keep using the unique_ptr
constructor.
gcc/rust/
* util/rust-make-unique.h: New.
This performs the lowering of the AST to HIR. The interesting piece here is
that we desugar much of the AST as mentioned in the previous pass, but
crucially, we also strip out all code that is "marked-for-strip" which failed
cfg-expansion from the expansion pass. After this, the HIR includes all code
required to compile for this crate.
gcc/rust/
* hir/rust-ast-lower-base.cc: New.
* hir/rust-ast-lower-base.h: New.
* hir/rust-ast-lower-block.h: New.
* hir/rust-ast-lower-enumitem.h: New.
* hir/rust-ast-lower-expr.h: New.
* hir/rust-ast-lower-extern.h: New.
* hir/rust-ast-lower-implitem.h: New.
* hir/rust-ast-lower-item.cc: New.
* hir/rust-ast-lower-item.h: New.
* hir/rust-ast-lower-pattern.cc: New.
* hir/rust-ast-lower-pattern.h: New.
* hir/rust-ast-lower-stmt.h: New.
* hir/rust-ast-lower-struct-field-expr.h: New.
* hir/rust-ast-lower-type.h: New.
* hir/rust-ast-lower.cc: New.
* hir/rust-ast-lower.h: New.
* hir/rust-hir-dump.cc: New.
* hir/rust-hir-dump.h: New.
This patch implements the classes mentioned in the previous HIR patch,
as well as a set of visitor frameworks used in handling that HIR.
gcc/rust/
* hir/tree/rust-hir-full-decls.h: New.
* hir/tree/rust-hir-full-test.cc: New.
* hir/tree/rust-hir-full.h: New.
* hir/tree/rust-hir-visitor.h: New.
* hir/tree/rust-hir.h: New.
This patch contains the declarations needed for our second intermediate
representation, which we will refer to as an HIR.
This gives the front-end a chance to desugar much of the AST, such as:
- Removing distinction between functions and methods
- Removing Macros
- Removing IdentifierExprs
- Removing duplicate attribute structures
gcc/rust/
* hir/tree/rust-hir-expr.h: New.
* hir/tree/rust-hir-item.h: New.
* hir/tree/rust-hir-path.h: New.
* hir/tree/rust-hir-pattern.h: New.
* hir/tree/rust-hir-stmt.h: New.
* hir/tree/rust-hir-type.h: New.
The name resolution is split into two phases, one toplevel pass which scans
the whole "Crate" which iterates all items and nested items in modules to
generate a context class full of CanonicalPath items. It also generates
a hierarchy of parent->child and child->parent relationships using the AST
NodeId for PathResolution in the second phase.
The second phase drills into each item like functions and creates a stack
of canonical paths for variables etc so that we can store information in
a side table of usage variable 'a' resolves to NodeId '123' which refers
to the NodeId of the "let a;" statement.
gcc/rust/
* resolve/rust-ast-resolve-base.cc: New.
* resolve/rust-ast-resolve-base.h: New.
* resolve/rust-ast-resolve-expr.cc: New.
* resolve/rust-ast-resolve-expr.h: New.
* resolve/rust-ast-resolve-implitem.h: New.
* resolve/rust-ast-resolve-item.cc: New.
* resolve/rust-ast-resolve-item.h: New.
* resolve/rust-ast-resolve-path.cc: New.
* resolve/rust-ast-resolve-path.h: New.
* resolve/rust-ast-resolve-pattern.cc: New.
* resolve/rust-ast-resolve-pattern.h: New.
* resolve/rust-ast-resolve-stmt.cc: New.
* resolve/rust-ast-resolve-stmt.h: New.
* resolve/rust-ast-resolve-struct-expr-field.cc: New.
* resolve/rust-ast-resolve-struct-expr-field.h: New.
* resolve/rust-ast-resolve-toplevel.h: New.
* resolve/rust-ast-resolve-type.cc: New.
* resolve/rust-ast-resolve-type.h: New.
* resolve/rust-ast-resolve.cc: New.
* resolve/rust-ast-resolve.h: New.
* resolve/rust-ast-verify-assignee.h: New.
* resolve/rust-name-resolver.cc: New.
* resolve/rust-name-resolver.h: New.
The expansion pass is responsible for two actions on our AST:
1. Expanding macro calls
2. Performing conditional compilation
Calls to macros should be checked and expanded into an AST fragment based on
the context they've been called in. This is similar to token substitution, with
a lot of intricacies and checks being performed. A single invocation can result
in an AST fragment containing multiple statements or multiple expressions,
which need to be handled as well. Furthermore, Rust macros can contain
repetitions relying on Kleine operators, similar to regular expression
patterns, that also need to be expanded properly.
Finally, Rust code can be hidden behind `cfg` directives, which allow the user
to perform conditional compilation. If a `cfg` predicate is not met, the
expression or statement it refers to should be marked for strip and removed
from the AST.
gcc/rust/
* expand/rust-attribute-visitor.cc: New.
* expand/rust-attribute-visitor.h: New.
* expand/rust-macro-builtins.cc: New.
* expand/rust-macro-builtins.h: New.
* expand/rust-macro-expand.cc: New.
* expand/rust-macro-expand.h: New.
* expand/rust-macro-invoc-lexer.cc: New.
* expand/rust-macro-invoc-lexer.h: New.
* expand/rust-macro-substitute-ctx.cc: New.
* expand/rust-macro-substitute-ctx.h: New.
Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Co-authored-by: Joel Phillips <simplytheother@gmail.com>
Signed-off-by: Joel Phillips <simplytheother@gmail.com>
This patch contains the second half of the templated Rust parser.
gcc/rust/
* parse/rust-parse-impl.h: New, second half.
Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
Signed-off-by: Joel Phillips <simplytheother@gmail.com>
This is a Pratt-style parser for Rust that implements all of the AST. The
rust-parser-impl.h is the implementation of the parser as a template,
allowing it to be given ManagedTokenSource and avoid virtual calls. The
downside is it takes time to compile when used.
see: https://en.wikipedia.org/wiki/Operator-precedence_parser#Pratt_parsing
This patch contains the first half of the templated parser, so as to not
lose patches in the mailing list archives.
gcc/rust/
* parse/rust-cfg-parser.cc: New.
* parse/rust-cfg-parser.h: New.
* parse/rust-parse-impl.h: New.
* parse/rust-parse.cc: New.
* parse/rust-parse.h: New.
Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
Signed-off-by: Joel Phillips <simplytheother@gmail.com>
The lexer is referred to as a ManagedTokenSource within the parser. This
lexer does not currently support Unicode, but serves as a starting point
to do so.
gcc/rust/
* lex/rust-codepoint.h: New.
* lex/rust-lex.cc: New.
* lex/rust-lex.h: New.
* lex/rust-token.cc: New.
* lex/rust-token.h: New.
* rust-buffered-queue.h: New.
Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
Co-authored-by: Mark Wielaard <mark@klomp.org>
Signed-off-by: Joel Phillips <simplytheother@gmail.com>