Skip to content

Commit 5a60504

Browse files
committed
[Heavy] Add value? for mlir.value with mlir.type
1 parent c92c664 commit 5a60504

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

heavy/lib/Mlir.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ heavy::ExternFunction parent_op;
6262
heavy::ExternFunction op_next;
6363
heavy::ExternFunction verify;
6464
heavy::ExternFunction module_lookup;
65+
heavy::ExternFunction is_value;
6566
}
6667

6768
using namespace heavy::mlir_helper;
@@ -533,7 +534,7 @@ void type(Context& C, ValueRefs Args) {
533534
mlir::MLIRContext* MLIRContext = getCurrentContext(C);
534535
llvm::StringRef TypeStr = Args[0].getStringRef();
535536
if (TypeStr.empty())
536-
return C.RaiseError("expecting string");
537+
return C.RaiseError("expecting string with type");
537538

538539
mlir::Type Type = mlir::parseType(TypeStr, MLIRContext,
539540
nullptr, heavy::String::IsNullTerminated);
@@ -652,7 +653,7 @@ void string_attr(Context& C, ValueRefs Args) {
652653
return C.RaiseError("invalid arity");
653654
if (!isa<heavy::String, heavy::Symbol>(Args[0]))
654655
return C.RaiseError("expecting string-like object");
655-
llvm::StringRef Str = Args[0].getStringRef();
656+
llvm::StringRef Str = Args[0].getStringRef();
656657
mlir::MLIRContext* MLIRContext = getCurrentContext(C);
657658
mlir::Attribute Attr = AttrTy::get(MLIRContext, Str);
658659
C.Cont(C.CreateAny(Attr));
@@ -801,6 +802,35 @@ void module_lookup(Context& C, heavy::ValueRefs Args) {
801802
C.Cont(Result);
802803
}
803804

805+
// (value? obj)
806+
// (value? _typestr_ obj)
807+
// (value? mlir.type obj)
808+
void is_value(Context& C, ValueRefs Args) {
809+
if (Args.size() < 1 || Args.size() > 2)
810+
return C.RaiseError("invalid arity");
811+
812+
mlir::MLIRContext* MLIRContext = getCurrentContext(C);
813+
mlir::Type Type;
814+
815+
if (Args.size() == 2) {
816+
if (isa<String, Symbol>(Args[0])) {
817+
llvm::StringRef TypeStr = Args[0].getStringRef();
818+
Type = mlir::parseType(TypeStr, MLIRContext,
819+
nullptr, heavy::String::IsNullTerminated);
820+
} else {
821+
Type = any_cast<mlir::Type>(Args[0]);
822+
}
823+
824+
if (!Type)
825+
return C.RaiseError("expecting a mlir.type");
826+
Args = Args.drop_front();
827+
}
828+
829+
mlir::Value Value = any_cast<mlir::Value>(Args[0]);
830+
bool Result = Value && (!Type || Value.getType() == Type);
831+
C.Cont(heavy::Bool(Result));
832+
}
833+
804834
} // namespace heavy::mlir_bind
805835

806836
extern "C" {
@@ -839,6 +869,7 @@ void HEAVY_MLIR_INIT(heavy::Context& C) {
839869
HEAVY_MLIR_VAR(with_new_context) = heavy::mlir_bind::with_new_context;
840870
HEAVY_MLIR_VAR(verify) = heavy::mlir_bind::verify;
841871
HEAVY_MLIR_VAR(module_lookup) = heavy::mlir_bind::module_lookup;
872+
HEAVY_MLIR_VAR(is_value) = heavy::mlir_bind::is_value;
842873
}
843874

844875
void HEAVY_MLIR_LOAD_MODULE(heavy::Context& C) {
@@ -871,6 +902,7 @@ void HEAVY_MLIR_LOAD_MODULE(heavy::Context& C) {
871902
{"load-dialect", HEAVY_MLIR_VAR(load_dialect)},
872903
{"verify", HEAVY_MLIR_VAR(verify)},
873904
{"module-lookup", HEAVY_MLIR_VAR(module_lookup)},
905+
{"value?", HEAVY_MLIR_VAR(is_value)},
874906
});
875907
}
876908
}

heavy/test/Nbdl/match_params.scm

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,25 @@
3939
(result
4040
(create-op "nbdl.get"
4141
(operands store key1)
42-
(result-types !nbdl.opaque))))
42+
(result-types !nbdl.store))))
4343
(define value
4444
(result
4545
(create-op "nbdl.get"
4646
(operands store key2)
47-
(result-types !nbdl.opaque))))
47+
(result-types !nbdl.store))))
4848
(define the-match-op
4949
(create-op "nbdl.match"
5050
(regions 1)
5151
(operands value key3)))
52+
(if (value? "!nbdl.opaque" key3)
53+
'ok
54+
(error "key should be a an opaque type"))
55+
(if (value? !nbdl.store foo)
56+
'ok
57+
(error "foo should be a nbdl store"))
58+
(if (value? value)
59+
'ok
60+
(error "value should be a value"))
5261
(with-builder
5362
(lambda ()
5463
(at-block-begin (entry-block the-match-op))

0 commit comments

Comments
 (0)