Project Stage2 - Part II
Tags: spo600
Published: 2023-11-14 20:00:57

In this blog, I will be designing how an automatic ifunc implementation within the GCC compiler could work from the user's point of view.

**_What options should the user specify on the command line to indicate that they want automatic ifunc capability to be applied during the compilation?_**

-> User can specify `-fifunc` on the command line, which triggers the GCC compiler to perform automatic ifunc resolution.

**_How should the user specify the list of architecture variants they want to target?_**

-> User should use `-march-variants=<arch1>,<arch2>...` on the command line. For example, it can be used like below. 
```shell
-march-variants=armv8-a,armv8-a+sve,armv8-a+sve2
```

**_What constraints need to be applied to the list of architecture variants? (i.e., how do we figure out if the architecture variants can be combined into a sane executable)?_**

-> List of constraints to ensure that specified variants can be combined into a functional executable:

- Compatibility: Each architecture variant should be compatible with the others.
- Target Hardware Support: The target hardware must support the architecture variants specified by the user.
- Runtime Support: The runtime environment should also support the specified architecture variants.
- Consistency with Instruction Set Architecture (ISA): Specified architecture variants should be consistent with the ISA.

When some of these constraints are not met, it can lead to an incoherent executable.

**_To which functions should the automatic ifunc capability be added? Should the user specify the functions to be affected? If so, how? (What should the source code syntax look like?)_**

-> user can explicitly specify which function should have automatic ifunc capability by using a source code annotation as below. 

```shell
__attribute__((ifunc("resolver_function")))
void target_function() {}
```

**_If the functions should be selected automatically, what should the selection criteria be? And should the user be able to tune the selection criteria, and if so, how?_**

-> Selection Criteria:
- Function Size: Smaller functions are preferable for optimization.
- Frequency of Function Calls: Functions called more often during execution are better candidates.
- Performance Guidelines: Potential functions can be selected based on rules applied to make informed decisions for optimizing the code.

-> Tuning the selection criteria:
- users can tune the automatic function selection criteria by specifying the criteria on the command line as below. 

```shell
-ffunc-selection-criteria=<c1>,<c2>,...

// example
-ffunc-selection-criteria=performance,size
```

**_What diagnostics should be produced during the compilation process?_**

-> Possible Feedback About Potential Errors or Important Information:

- Incompatible Architecture Variants Warning: Users will receive a warning if the specified architecture variants are not compatible with each other.
  
- Check Hardware Support: Users will be alerted if the target hardware does not support the specified architecture variants.

- Check Runtime Support: Users will be warned if the specified architecture variants are not supported by the runtime environment.

- Failure to Apply Automatic Ifunc Capability: Users will encounter an error if the compiler fails to resolve ifunc for a function.

- Information About the List of Architecture Variants Specified by the User: Users will be informed about the architecture variants they specified.

- Information About Function Selection: Users will receive information about which functions were selected for ifunc capability.

- Information About Selection Criteria: Users will be informed about the list of selection criteria they tuned.


**Explain your design decisions in detail, clearly presenting the case for each decision and why you feel it's the best option.**

**_1. `-fifunc` option for Automatic Ifunc Resolution_**
   - The `-f` prefix is commonly used in the design of GCC, making the `-fifunc` option consistent with existing conventions in GCC.
   - The option name is straightforward and meaningful.
   - Users can seamlessly integrate automatic ifunc capability into the compilation process with the `-fifunc` option.

**_2. `-march-variants=<arch1>,<arch2>...` option for Specifying Targeted Architecture Variants_**
   - The `-march-variants` option is consistent with the existing GCC syntax, specifically `-march`.
   - Users can easily specify multiple target architecture variants using a comma-separated list.
   - This design is adaptable for other platforms as well.

**_3. Constraints_**
   - Avoiding unpredictable outcomes is achievable by considering compatibility, hardware and runtime support, and maintaining consistency with the Instruction Set Architecture (ISA).

**_4. Manual Specification by User_**
   - The use of `__attribute__((ifunc("resolver_function")))` is consistent with the existing conventions of GCC.
   - Users have direct control over ifunc behaviors through manual specification.

**_5. Automatic Function Selection Criteria_**
   - The `ffunc-selection-criteria` is intuitive and consistent with existing GCC options.
   - Allowing users to adjust the selection criteria provides flexibility.
   - If a user does not specify any selection criteria, default criteria will be used.