Skip to content

Commit 4008cb9

Browse files
committed
[WIP][LLVM] Add __builtin_readfixedtimer intrinsic and buiiltin
Summary: This patch adds a new intrinsic and builtin function mirroring the existing `__builtin_readcyclecounter`. The difference is that this implementation targets a separate counter that some targets have which returns a fixed frequency clock that can be used to determine elapsed time, this is different compared to the cycle counter which often has variable frequency. This is currently only valid for the NVPTX and AMDGPU targets.
1 parent 9673741 commit 4008cb9

File tree

19 files changed

+82
-6
lines changed

19 files changed

+82
-6
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,12 @@ def ReadCycleCounter : Builtin {
11101110
let Prototype = "unsigned long long int()";
11111111
}
11121112

1113+
def ReadFixedTimer : Builtin {
1114+
let Spellings = ["__builtin_readfixedtimer"];
1115+
let Attributes = [NoThrow];
1116+
let Prototype = "unsigned long long int()";
1117+
}
1118+
11131119
def Trap : Builtin {
11141120
let Spellings = ["__builtin_trap"];
11151121
let Attributes = [NoThrow, NoReturn];

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3443,6 +3443,10 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
34433443
Function *F = CGM.getIntrinsic(Intrinsic::readcyclecounter);
34443444
return RValue::get(Builder.CreateCall(F));
34453445
}
3446+
case Builtin::BI__builtin_readfixedtimer: {
3447+
Function *F = CGM.getIntrinsic(Intrinsic::readfixedtimer);
3448+
return RValue::get(Builder.CreateCall(F));
3449+
}
34463450
case Builtin::BI__builtin___clear_cache: {
34473451
Value *Begin = EmitScalarExpr(E->getArg(0));
34483452
Value *End = EmitScalarExpr(E->getArg(1));

llvm/include/llvm/CodeGen/ISDOpcodes.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,12 @@ enum NodeType {
11791179
/// counter-like register (or other high accuracy low latency clock source).
11801180
READCYCLECOUNTER,
11811181

1182+
/// READFIXEDTIMER - This corresponds to the readfixedcounter intrinsic.
1183+
/// It has the same semantics as the READCYCLECOUNTER implementation except
1184+
/// that the result is the content of the architecture-specific fixed
1185+
/// frequency counter suitable for measuring elapsed time.
1186+
READFIXEDTIMER,
1187+
11821188
/// HANDLENODE node - Used as a handle for various purposes.
11831189
HANDLENODE,
11841190

llvm/include/llvm/IR/Intrinsics.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,8 @@ def int_pcmarker : DefaultAttrsIntrinsic<[], [llvm_i32_ty]>;
870870

871871
def int_readcyclecounter : DefaultAttrsIntrinsic<[llvm_i64_ty]>;
872872

873+
def int_readfixedtimer : DefaultAttrsIntrinsic<[llvm_i64_ty]>;
874+
873875
// The assume intrinsic is marked InaccessibleMemOnly so that proper control
874876
// dependencies will be maintained.
875877
def int_assume : DefaultAttrsIntrinsic<

llvm/include/llvm/Support/TargetOpcodes.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ HANDLE_TARGET_OPCODE(G_INTRINSIC_ROUNDEVEN)
352352
/// INTRINSIC readcyclecounter
353353
HANDLE_TARGET_OPCODE(G_READCYCLECOUNTER)
354354

355+
/// INTRINSIC readfixedtimer
356+
HANDLE_TARGET_OPCODE(G_READFIXEDTIMER)
357+
355358
/// Generic load (including anyext load)
356359
HANDLE_TARGET_OPCODE(G_LOAD)
357360

llvm/include/llvm/Target/GenericOpcodes.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,12 @@ def G_READCYCLECOUNTER : GenericInstruction {
11011101
let hasSideEffects = true;
11021102
}
11031103

1104+
def G_READFIXEDTIMER : GenericInstruction {
1105+
let OutOperandList = (outs type0:$dst);
1106+
let InOperandList = (ins);
1107+
let hasSideEffects = true;
1108+
}
1109+
11041110
//------------------------------------------------------------------------------
11051111
// Memory ops
11061112
//------------------------------------------------------------------------------

llvm/include/llvm/Target/GlobalISel/SelectionDAGCompat.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ def : GINodeEquiv<G_FMAXNUM_IEEE, fmaxnum_ieee>;
167167
def : GINodeEquiv<G_FMAXIMUM, fmaximum>;
168168
def : GINodeEquiv<G_FMINIMUM, fminimum>;
169169
def : GINodeEquiv<G_READCYCLECOUNTER, readcyclecounter>;
170+
def : GINodeEquiv<G_READFIXEDTIMER, readfixedtimer>;
170171
def : GINodeEquiv<G_ROTR, rotr>;
171172
def : GINodeEquiv<G_ROTL, rotl>;
172173
def : GINodeEquiv<G_LROUND, lround>;

llvm/include/llvm/Target/TargetSelectionDAG.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,9 @@ def prefetch : SDNode<"ISD::PREFETCH" , SDTPrefetch,
657657
def readcyclecounter : SDNode<"ISD::READCYCLECOUNTER", SDTIntLeaf,
658658
[SDNPHasChain, SDNPSideEffect]>;
659659

660+
def readfixedtimer : SDNode<"ISD::READFIXEDTIMER", SDTIntLeaf,
661+
[SDNPHasChain, SDNPSideEffect]>;
662+
660663
def membarrier : SDNode<"ISD::MEMBARRIER", SDTNone,
661664
[SDNPHasChain, SDNPSideEffect]>;
662665

llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,8 @@ unsigned IRTranslator::getSimpleIntrinsicOpcode(Intrinsic::ID ID) {
18851885
return TargetOpcode::G_INTRINSIC_TRUNC;
18861886
case Intrinsic::readcyclecounter:
18871887
return TargetOpcode::G_READCYCLECOUNTER;
1888+
case Intrinsic::readfixedtimer:
1889+
return TargetOpcode::G_READFIXEDTIMER;
18881890
case Intrinsic::ptrmask:
18891891
return TargetOpcode::G_PTRMASK;
18901892
case Intrinsic::lrint:

llvm/lib/CodeGen/IntrinsicLowering.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,12 @@ void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
312312
CI->replaceAllUsesWith(ConstantInt::get(Type::getInt64Ty(Context), 0));
313313
break;
314314
}
315+
case Intrinsic::readfixedtimer: {
316+
errs() << "WARNING: this target does not support the llvm.readfixedtimer"
317+
<< " intrinsic. It is being lowered to a constant 0\n";
318+
CI->replaceAllUsesWith(ConstantInt::get(Type::getInt64Ty(Context), 0));
319+
break;
320+
}
315321

316322
case Intrinsic::dbg_declare:
317323
case Intrinsic::dbg_label:

0 commit comments

Comments
 (0)