Skip to content
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

Compile pattern match statements into conditional statements #2705

Merged
merged 1 commit into from
Nov 1, 2023

Conversation

powerboat9
Copy link
Contributor

No description provided.

gcc/rust/ChangeLog:

	* backend/rust-compile-expr.cc
	(patterns_mergeable): Remove.
	(struct PatternMerge): Remove.
	(sort_tuple_patterns): Remove.
	(simplify_tuple_match): Remove.
	(CompileExpr::visit): Modify compilation of MatchExpr.

	* backend/rust-compile-pattern.cc
	(CompilePatternCaseLabelExpr::visit): Replace with...
	(CompilePatternCheckExpr::visit): ...this.
	* backend/rust-compile-pattern.h
	(class CompilePatternCaseLabelExpr): Replace with...
	(class CompilePatternCheckExpr): ...this.

	* backend/rust-compile-type.cc
	(TyTyResolveCompile::get_implicit_enumeral_node_type):
	Make enumeral type equivalent to isize.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
Copy link
Member

@philberty philberty left a comment

Choose a reason for hiding this comment

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

This looks reallly good! There is an optimization in gimple to turn conditionals into switches I am wondering if this is getting triggered? You should be able to do -fdump-tree-all and take a look on some of the simple examples to see if this is happening. I will finish the review over the course of the day.

We really need this so thanks for doing it!

@powerboat9
Copy link
Contributor Author

pub fn main() -> i32 {
    0
}

pub fn foo(v: i32) -> i32 {
    match (v) {
	0 => 2,
	1 => 3,
	2 => 5,
	3 => 7,
	4 => 11,
	5 => 13,
	6 => 17,
	7 => 19
    }
}

;; Function test::main (main, funcdef_no=0, decl_uid=75, cgraph_uid=1, symbol_order=0)

__attribute__((cdecl))
i32 test::main ()
{
  <bb 2> :
  return 0;

}



;; Function test::foo (_ZN4test3foo17heacd8d0bc9bba643E, funcdef_no=1, decl_uid=82, cgraph_uid=2, symbol_order=1)

;; Canonical GIMPLE case clusters: 0 1 2 3 4 5 6 7 
;; JT can be built: JT:0-7 
Removing basic block 3
Removing basic block 4
Removing basic block 5
Removing basic block 6
Removing basic block 7
Removing basic block 8
Removing basic block 9
Expanded into a new gimple STMT: switch (v_2(D)) <default: <L26> [INV], case 0: <L18> [INV], case 1: <L19> [INV], case 2: <L20> [INV], case 3: <L21> [INV], case 4: <L22> [INV], case 5: <L23> [INV], case 6: <L24> [INV], case 7: <L25> [INV]>

__attribute__((cdecl))
i32 test::foo (const i32 v)
{
  i32 RUSTTMP.2;
  i32 RUSTTMP.2_1;

  <bb 2> :
  switch (v_2(D)) <default: <L26> [INV], case 0: <L18> [INV], case 1: <L19> [INV], case 2: <L20> [INV], case 3: <L21> [INV], case 4: <L22> [INV], case 5: <L23> [INV], case 6: <L24> [INV], case 7: <L25> [INV]>

  <bb 3> :
<L18>:
  goto <bb 11>; [100.00%]

  <bb 4> :
<L19>:
  goto <bb 11>; [100.00%]

  <bb 5> :
<L20>:
  goto <bb 11>; [100.00%]

  <bb 6> :
<L21>:
  goto <bb 11>; [100.00%]

  <bb 7> :
<L22>:
  goto <bb 11>; [100.00%]

  <bb 8> :
<L23>:
  goto <bb 11>; [100.00%]

  <bb 9> :
<L24>:
  goto <bb 11>; [100.00%]

  <bb 10> :
<L25>:

  <bb 11> :
  # RUSTTMP.2_1 = PHI <2(3), 3(4), 5(5), 7(6), 11(7), 13(8), 17(9), 19(10), RUSTTMP.2_4(D)(2)>
<L26>:
  return RUSTTMP.2_1;

}

Copy link
Member

@philberty philberty left a comment

Choose a reason for hiding this comment

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

LGTM lets go for this i am very excited and happy to see this go in!

@philberty philberty added this pull request to the merge queue Nov 1, 2023
Merged via the queue into Rust-GCC:master with commit e489a69 Nov 1, 2023
9 checks passed
@powerboat9 powerboat9 deleted the redo-match branch November 1, 2023 20:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants