Skip to content

Commit aaea92e

Browse files
committed
[mlir] Reintroduce nano time to execution_engine
Prior change had a broken test that wasn't run by accident. Reviewed By: ftynse Differential Revision: https://reviews.llvm.org/D113488
1 parent 52da6f5 commit aaea92e

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

mlir/lib/ExecutionEngine/RunnerUtils.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//===----------------------------------------------------------------------===//
1515

1616
#include "mlir/ExecutionEngine/RunnerUtils.h"
17+
#include <chrono>
1718

1819
extern "C" void
1920
_mlir_ciface_print_memref_shape_i8(UnrankedMemRefType<int8_t> *M) {
@@ -75,6 +76,14 @@ extern "C" void _mlir_ciface_print_memref_f64(UnrankedMemRefType<double> *M) {
7576
impl::printMemRef(*M);
7677
}
7778

79+
extern "C" int64_t _mlir_ciface_nano_time() {
80+
auto now = std::chrono::high_resolution_clock::now();
81+
auto duration = now.time_since_epoch();
82+
auto nanoseconds =
83+
std::chrono::duration_cast<std::chrono::nanoseconds>(duration);
84+
return nanoseconds.count();
85+
}
86+
7887
extern "C" void print_memref_i32(int64_t rank, void *ptr) {
7988
UnrankedMemRefType<int32_t> descriptor = {rank, ptr};
8089
_mlir_ciface_print_memref_i32(&descriptor);

mlir/test/python/execution_engine.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,37 @@ def testSharedLibLoad():
358358

359359

360360
run(testSharedLibLoad)
361+
362+
363+
# Test that nano time clock is available.
364+
# CHECK-LABEL: TEST: testNanoTime
365+
def testNanoTime():
366+
with Context():
367+
module = Module.parse("""
368+
module {
369+
func @main() attributes { llvm.emit_c_interface } {
370+
%now = call @nano_time() : () -> i64
371+
%memref = memref.alloca() : memref<1xi64>
372+
%c0 = arith.constant 0 : index
373+
memref.store %now, %memref[%c0] : memref<1xi64>
374+
%u_memref = memref.cast %memref : memref<1xi64> to memref<*xi64>
375+
call @print_memref_i64(%u_memref) : (memref<*xi64>) -> ()
376+
return
377+
}
378+
func private @nano_time() -> i64 attributes { llvm.emit_c_interface }
379+
func private @print_memref_i64(memref<*xi64>) attributes { llvm.emit_c_interface }
380+
}""")
381+
382+
execution_engine = ExecutionEngine(
383+
lowerToLLVM(module),
384+
opt_level=3,
385+
shared_libs=[
386+
"../../../../lib/libmlir_runner_utils.so",
387+
"../../../../lib/libmlir_c_runner_utils.so"
388+
])
389+
execution_engine.invoke("main")
390+
# CHECK: Unranked Memref
391+
# CHECK: [{{.*}}]
392+
393+
394+
run(testNanoTime)

0 commit comments

Comments
 (0)