@@ -1127,30 +1127,52 @@ TEST(FfiTest, WrongEnumAttrType) {
11271127 << status.message () << " \n " ;
11281128}
11291129
1130- struct MyData {
1130+ struct MyDataWithAutoTypeId {
11311131 static TypeId id;
1132- std::string str ;
1132+ std::string value ;
11331133};
11341134
1135- TypeId MyData::id = {}; // zero-initialize type id
1136- XLA_FFI_REGISTER_TYPE (GetXlaFfiApi(), " my_data" , &MyData::id);
1135+ struct MyDataWithExplicitTypeId {
1136+ static TypeId id;
1137+ int64_t value;
1138+ };
1139+
1140+ // Rely on XLA to assign unique type id for the type.
1141+ TypeId MyDataWithAutoTypeId::id = XLA_FFI_UNKNOWN_TYPE_ID;
1142+ XLA_FFI_REGISTER_TYPE (GetXlaFfiApi(), " my_data_auto" ,
1143+ &MyDataWithAutoTypeId::id);
1144+
1145+ // Provide explicit type id and rely on XLA to check that it's unique.
1146+ TypeId MyDataWithExplicitTypeId::id = {42 };
1147+ XLA_FFI_REGISTER_TYPE (GetXlaFfiApi(), " my_data_explicit" ,
1148+ &MyDataWithExplicitTypeId::id);
11371149
11381150TEST (FfiTest, UserData) {
1139- MyData data{" foo" };
1151+ MyDataWithAutoTypeId data0{" foo" };
1152+ MyDataWithExplicitTypeId data1{42 };
1153+
1154+ EXPECT_GE (MyDataWithAutoTypeId::id.type_id , 0 );
1155+ EXPECT_EQ (MyDataWithExplicitTypeId::id.type_id , 42 );
11401156
11411157 ExecutionContext execution_context;
11421158 TF_ASSERT_OK (execution_context.Insert (
1143- TypeIdRegistry::TypeId (MyData::id.type_id ), &data));
1159+ TypeIdRegistry::TypeId (MyDataWithAutoTypeId::id.type_id ), &data0));
1160+ TF_ASSERT_OK (execution_context.Insert (
1161+ TypeIdRegistry::TypeId (MyDataWithExplicitTypeId::id.type_id ), &data1));
11441162
11451163 CallFrameBuilder builder (/* num_args=*/ 0 , /* num_rets=*/ 0 );
11461164 auto call_frame = builder.Build ();
11471165
1148- auto fn = [&](MyData* data) {
1149- EXPECT_EQ (data->str , " foo" );
1166+ auto fn = [&](MyDataWithAutoTypeId* data0, MyDataWithExplicitTypeId* data1) {
1167+ EXPECT_EQ (data0->value , " foo" );
1168+ EXPECT_EQ (data1->value , 42 );
11501169 return Error::Success ();
11511170 };
11521171
1153- auto handler = Ffi::Bind ().Ctx <UserData<MyData>>().To (fn);
1172+ auto handler = Ffi::Bind ()
1173+ .Ctx <UserData<MyDataWithAutoTypeId>>()
1174+ .Ctx <UserData<MyDataWithExplicitTypeId>>()
1175+ .To (fn);
11541176
11551177 CallOptions options;
11561178 options.execution_context = &execution_context;
0 commit comments