-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Alt optimizations #1897
Copy link
Copy link
Closed
Labels
C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.E-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.Call for participation: Hard difficulty. Experience needed to fix: A lot.I-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.
Metadata
Metadata
Assignees
Labels
C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.E-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.Call for participation: Hard difficulty. Experience needed to fix: A lot.I-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.
Type
Fields
Give feedbackNo fields configured for issues without a type.
Let's take advantage of enum types and alt-expressions to optimize code that uses
alt. Some of these optimizations might require teaching LLVM about the restrictions on enums, or doing the work ourselves in trans_alt.Algorithm selection
Based on the cases, pick a map structure:
i = (x - smallest)i = perfecthash(x)iin a hash tableBased on the branches, pick an output type:
ias an index into an array of valuesi * block_lengthias an index into an array of jump offsetsIn this example, the cases are a dense enum and the branches are values:
So it can use an array of values, with no branching (even for a bounds check):
This is something Mozilla programmers do in C++ all the time, manually, often messing it up.
Optimizations for if/elif/else compilation
Omit the last "else if" check when the alt is exhaustive, turning these into a simple "if":
Reorder cases in order to put a difficult case at the end (where it can be omitted):
Collapse adjacent cases into a single less-than check:
"Pass through" optimization
Compile as "pass through" if the arms all match the values. Useful when translating between two enum types.