Skip to content

Commit 095dff8

Browse files
authored
Add AMX and other Apple proprietary instructions. (#78)
* Add docs how to add a new processor extension. * Remove { for AArch64 instructions. * Add Apple proprietary AMX extension. * Add Mul53 instructions. * Add missing mayLoad and mayStore flags. * Add gexit, genter, wkdmd, wkdmc instructions. * Add sdsb and at_as1elx
1 parent 26bc587 commit 095dff8

28 files changed

Lines changed: 374 additions & 73 deletions

AddExtensions.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Adding new extensions
2+
3+
Some architectures have processor extensions not defined in upstream LLVM.
4+
If we want to have them in Capstone, we can write the `.td` files for them here.
5+
6+
The process is rather straight forward.
7+
8+
1. Write all definitions into a new `llvm/lib/Target/<ARCH>/<ARCH>NewExtension.td` file.
9+
2. Define a feature and property for the new extension:
10+
```python
11+
def FeatureExtensionName : SubtargetFeature<"extenion_name", "HasExtensionName", "true",
12+
"Enable the new extension bla bla bla.">;
13+
14+
def HasExtensionName : Predicate<"Subtarget->hasExtensionName()">,
15+
AssemblerPredicate<(all_of FeatureExtensionName), "extenion_name">;
16+
```
17+
3. Ensure the newly defined instructions have `let Predicates = [HasExtensionName];` set.
18+
4. Add or exclude the extension from existing processor models:
19+
- Search for `list<Predicate> UnsupportedFeatures` in the target directory.
20+
Add `HasExtensionName` to all processor models which don't support it.
21+
- The processor models which do support it, need the instructions in the scheduler.
22+
Check how it is done in the processor's scheduler file.
23+
5. Include the `<ARCH>NewExtension.td` at the bottom of `<ARCH>InstrInfo.td` with `include "<ARCH>NewExtension.td"`.
24+
25+
**Note:** As an example you can refer to `AArch64AppleProprietary.td`.
26+
Also search for `HasAMX` and `FeatureAMX` to see how those flags are used in the files.
27+
28+
# Deprecated Features
29+
30+
Capstone needs to support features which were removed by LLVM in the past.
31+
Here we explain how to reintroduce them.
32+
33+
## Reintroduction
34+
35+
To get the old features back we copy them from the old `.td` files and include them in the new ones.
36+
37+
To include removed features from previous LLVM versions do the following:
38+
39+
1. Checkout the last LLVM version the feature was present.
40+
2. Copy all feature related definitions into a `<ARCH>Deprecated.td` file.
41+
3. Checkout the newest LLVM version again.
42+
4. Wrap the different definition types in include guards. For example the `InstrInfo` definitions could be included in:
43+
44+
```
45+
#ifndef INCLUDED_CAPSTONE_DEPR_INSTR
46+
#ifdef CAPSTONE_DEPR_INSTR
47+
#define INCLUDED_CAPSTONE_DEPR_INSTR // Ensures it is only included once
48+
49+
[Instruction definitions of removed feature]
50+
51+
#endif // INCLUDED_CAPSTONE_DEPR_INSTR
52+
#endif // CAPSTONE_DEPR_INSTR
53+
```
54+
55+
_Note that the order of `#ifndef` and `#ifdef` matters (otherwise you'll get an error from `tblgen`)._
56+
57+
**This step is somewhat optional. It might be enough to write all definitions in a single file and `#include` them once into `ARCHInstrInfo.td` at the bottom.**
58+
59+
5. Include the definitions in the current definition files with:
60+
61+
```
62+
#define CAPSTONE_DEPR_INSTR
63+
include "<ARCH>Deprecated.td"
64+
```
65+
66+
## Notes
67+
- It is possible that you have to change some definitions slightly.
68+
Because certain classes no longer exist or were replaced (e.g.: `GCCBuiltin` -> `ClangBuiltin`).
69+
- Some processors might need the feature flag (`Has<DeprecatedFeature>`) added
70+
in their `UnsupportedFeatures` list.

DeprecatedFeatures.md

Lines changed: 0 additions & 41 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,4 @@ You need to search for the instruction or operands with missing or incorrect val
123123
```
124124

125125
- If certain target features (e.g. architecture extensions) were removed from LLVM or you want to add your own,
126-
checkout [DeprecatedFeatures.md](DeprecatedFeatures.md).
126+
checkout [AddExtensions.md](AddExtensions.md).

llvm/lib/Target/AArch64/AArch64.td

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,7 @@ def TuneAmpere1B : SubtargetFeature<"ampere1b", "ARMProcFamily", "Ampere1B",
13951395
FeatureLdpAlignedOnly,
13961396
FeatureStpAlignedOnly]>;
13971397

1398+
13981399
def ProcessorFeatures {
13991400
list<SubtargetFeature> A53 = [HasV8_0aOps, FeatureCRC, FeatureCrypto,
14001401
FeatureFPARMv8, FeatureNEON, FeaturePerfMon];
@@ -1467,35 +1468,56 @@ def ProcessorFeatures {
14671468
FeatureSVE, FeatureComplxNum];
14681469
list<SubtargetFeature> Carmel = [HasV8_2aOps, FeatureNEON, FeatureCrypto,
14691470
FeatureFullFP16];
1471+
1472+
// Capstone addition:
1473+
// Proprietary system instructions are added to all Apple processors.
1474+
// Even if we don't know if they are actually supported.
1475+
14701476
list<SubtargetFeature> AppleA7 = [HasV8_0aOps, FeatureCrypto, FeatureFPARMv8,
1471-
FeatureNEON,FeaturePerfMon, FeatureAppleA7SysReg];
1477+
FeatureNEON,FeaturePerfMon, FeatureAppleA7SysReg,
1478+
FeatureAppleSys];
14721479
list<SubtargetFeature> AppleA10 = [HasV8_0aOps, FeatureCrypto, FeatureFPARMv8,
14731480
FeatureNEON, FeaturePerfMon, FeatureCRC,
1474-
FeatureRDM, FeaturePAN, FeatureLOR, FeatureVH];
1481+
FeatureRDM, FeaturePAN, FeatureLOR, FeatureVH,
1482+
FeatureAppleSys];
1483+
1484+
// Capstone addition:
1485+
// Note about the Mul53 extension. According to https://gist.github.com/TrungNguyen1909/5b323edda9a21550a1621af506e8ce5f
1486+
// The mul53 instructions are use in Apple's H10 processor (part of AirPots according to apple.fandom.com).
1487+
// This processor doesn't exist in LLVM. But the reference claims H10 is equivalent to A11.
14751488
list<SubtargetFeature> AppleA11 = [HasV8_2aOps, FeatureCrypto, FeatureFPARMv8,
1476-
FeatureNEON, FeaturePerfMon, FeatureFullFP16];
1489+
FeatureNEON, FeaturePerfMon, FeatureFullFP16,
1490+
FeatureMUL53, FeatureAppleSys];
14771491
list<SubtargetFeature> AppleA12 = [HasV8_3aOps, FeatureCrypto, FeatureFPARMv8,
14781492
FeatureNEON, FeaturePerfMon, FeatureFullFP16];
14791493
list<SubtargetFeature> AppleA13 = [HasV8_4aOps, FeatureCrypto, FeatureFPARMv8,
14801494
FeatureNEON, FeaturePerfMon, FeatureFullFP16,
1481-
FeatureFP16FML, FeatureSHA3];
1495+
FeatureFP16FML, FeatureSHA3, FeatureAppleSys];
1496+
1497+
// Capstone addition AMX: The A14 onwards are variants (same generation)
1498+
// of the Apple M series processors.
1499+
// To my knowledge it is not documented if those processors actually support AMX,
1500+
// but we add it here as feature anyways. Simply because it is too much work
1501+
// adding a model for the Apple M series processors. Capstone currently
1502+
// doesn't care about the LLVM processor definitions.
14821503
list<SubtargetFeature> AppleA14 = [HasV8_4aOps, FeatureCrypto, FeatureFPARMv8,
14831504
FeatureNEON, FeaturePerfMon, FeatureFRInt3264,
14841505
FeatureSpecRestrict, FeatureSSBS, FeatureSB,
14851506
FeaturePredRes, FeatureCacheDeepPersist,
14861507
FeatureFullFP16, FeatureFP16FML, FeatureSHA3,
1487-
FeatureAltFPCmp];
1508+
FeatureAltFPCmp, FeatureAMX, FeatureAppleSys];
14881509
list<SubtargetFeature> AppleA15 = [HasV8_6aOps, FeatureCrypto, FeatureFPARMv8,
14891510
FeatureNEON, FeaturePerfMon, FeatureSHA3,
1490-
FeatureFullFP16, FeatureFP16FML];
1511+
FeatureFullFP16, FeatureFP16FML, FeatureAMX,
1512+
FeatureAppleSys];
14911513
list<SubtargetFeature> AppleA16 = [HasV8_6aOps, FeatureCrypto, FeatureFPARMv8,
14921514
FeatureNEON, FeaturePerfMon, FeatureSHA3,
14931515
FeatureFullFP16, FeatureFP16FML,
1494-
FeatureHCX];
1516+
FeatureHCX, FeatureAMX, FeatureAppleSys];
14951517
list<SubtargetFeature> AppleA17 = [HasV8_6aOps, FeatureCrypto, FeatureFPARMv8,
14961518
FeatureNEON, FeaturePerfMon, FeatureSHA3,
14971519
FeatureFullFP16, FeatureFP16FML,
1498-
FeatureHCX];
1520+
FeatureHCX, FeatureAMX, FeatureAppleSys];
14991521
list<SubtargetFeature> ExynosM3 = [HasV8_0aOps, FeatureCRC, FeatureCrypto,
15001522
FeaturePerfMon];
15011523
list<SubtargetFeature> ExynosM4 = [HasV8_2aOps, FeatureCrypto, FeatureDotProd,

0 commit comments

Comments
 (0)