Skip to content

Commit 74489ee

Browse files
[EVM] Handle target-cpu and target-features function attributes
Add support for handling target-features function attribute which can be used to set EVM version. Currently, target-cpu is not used, but maybe in a future it will be. Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
1 parent a3a150c commit 74489ee

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

llvm/lib/Target/EVM/EVMTargetMachine.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,29 @@ EVMTargetMachine::EVMTargetMachine(const Target &T, const Triple &TT,
8989
: CodeGenTargetMachineImpl(T, computeDataLayout(), TT, CPU, FS, Options,
9090
getEffectiveRelocModel(RM),
9191
getEffectiveCodeModel(CM, CodeModel::Small), OL),
92-
TLOF(std::make_unique<EVMELFTargetObjectFile>()),
93-
Subtarget(TT, std::string(CPU), std::string(FS), *this) {
92+
TLOF(std::make_unique<EVMELFTargetObjectFile>()) {
9493
initAsmInfo();
9594
}
9695

96+
const EVMSubtarget *
97+
EVMTargetMachine::getSubtargetImpl(const Function &F) const {
98+
Attribute CPUAttr = F.getFnAttribute("target-cpu");
99+
Attribute FSAttr = F.getFnAttribute("target-features");
100+
101+
auto CPU = CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
102+
auto FS = FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
103+
104+
auto &I = SubtargetMap[CPU + FS];
105+
if (!I) {
106+
// This needs to be done before we create a new subtarget since any
107+
// creation will depend on the TM and the code generation flags on the
108+
// function that reside in TargetOptions.
109+
resetTargetOptions(F);
110+
I = std::make_unique<EVMSubtarget>(TargetTriple, CPU, FS, *this);
111+
}
112+
return I.get();
113+
}
114+
97115
TargetTransformInfo
98116
EVMTargetMachine::getTargetTransformInfo(const Function &F) const {
99117
return TargetTransformInfo(std::make_unique<EVMTTIImpl>(this, F));

llvm/lib/Target/EVM/EVMTargetMachine.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace llvm {
2222
///
2323
class EVMTargetMachine final : public CodeGenTargetMachineImpl {
2424
std::unique_ptr<TargetLoweringObjectFile> TLOF;
25-
EVMSubtarget Subtarget;
25+
mutable StringMap<std::unique_ptr<EVMSubtarget>> SubtargetMap;
2626

2727
public:
2828
EVMTargetMachine(const Target &T, const Triple &TT, StringRef CPU,
@@ -33,9 +33,11 @@ class EVMTargetMachine final : public CodeGenTargetMachineImpl {
3333

3434
TargetPassConfig *createPassConfig(PassManagerBase &PM) override;
3535

36-
const EVMSubtarget *getSubtargetImpl(const Function &F) const override {
37-
return &Subtarget;
38-
}
36+
const EVMSubtarget *getSubtargetImpl(const Function &F) const override;
37+
// DO NOT IMPLEMENT: There is no such thing as a valid default subtarget,
38+
// subtargets are per-function entities based on the target-specific
39+
// attributes of each function.
40+
const EVMSubtarget *getSubtargetImpl() const = delete;
3941

4042
TargetTransformInfo getTargetTransformInfo(const Function &F) const override;
4143

llvm/test/CodeGen/EVM/clz-ctz.ll

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,37 @@ define i256 @clz(i256 %v) {
130130
ret i256 %res
131131
}
132132

133+
define i256 @clz_target_features(i256 %v) "target-features"="+osaka" {
134+
; GENERIC-LABEL: clz_target_features:
135+
; GENERIC: ; %bb.0:
136+
; GENERIC-NEXT: JUMPDEST
137+
; GENERIC-NEXT: CLZ
138+
; GENERIC-NEXT: SWAP1
139+
; GENERIC-NEXT: JUMP
140+
;
141+
; OSAKA-LABEL: clz_target_features:
142+
; OSAKA: ; %bb.0:
143+
; OSAKA-NEXT: JUMPDEST
144+
; OSAKA-NEXT: CLZ
145+
; OSAKA-NEXT: SWAP1
146+
; OSAKA-NEXT: JUMP
147+
%res = call i256 @llvm.ctlz.i256(i256 %v, i1 false)
148+
ret i256 %res
149+
}
150+
133151
define i256 @ctz(i256 %v) {
134152
; GENERIC-LABEL: ctz:
135153
; GENERIC: ; %bb.0:
136154
; GENERIC-NEXT: JUMPDEST
137155
; GENERIC-NEXT: DUP1
138-
; GENERIC-NEXT: PUSH4 @.BB1_2
156+
; GENERIC-NEXT: PUSH4 @.BB2_2
139157
; GENERIC-NEXT: JUMPI
140158
; GENERIC-NEXT: ; %bb.1:
141159
; GENERIC-NEXT: POP
142160
; GENERIC-NEXT: PUSH2 0x100
143161
; GENERIC-NEXT: SWAP1
144162
; GENERIC-NEXT: JUMP
145-
; GENERIC-NEXT: .BB1_2: ; %cond.false
163+
; GENERIC-NEXT: .BB2_2: ; %cond.false
146164
; GENERIC-NEXT: JUMPDEST
147165
; GENERIC-NEXT: PUSH32 0x101010101010101010101010101010101010101010101010101010101010101
148166
; GENERIC-NEXT: PUSH16 0xF0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F

0 commit comments

Comments
 (0)