Skip to content

Commit df58b4d

Browse files
authored
Set const for array passed or returned from C/C++ API (#2104)
* Set parameters returns of arrays like `shape`, `strides` and some TensorList pointers as `const` * Implements #2075
1 parent c53f36c commit df58b4d

File tree

11 files changed

+55
-41
lines changed

11 files changed

+55
-41
lines changed

include/onnx-mlir/Runtime/OMTensor.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
//===-------------- OMTensor.h - OMTensor Declaration header --------------===//
66
//
7-
// Copyright 2019-2020 The IBM Research Authors.
7+
// Copyright 2019-2023 The IBM Research Authors.
88
//
99
// =============================================================================
1010
//
@@ -90,7 +90,7 @@ extern "C" {
9090
*
9191
*/
9292
OM_EXTERNAL_VISIBILITY OMTensor *omTensorCreate(
93-
void *data_ptr, int64_t *shape, int64_t rank, OM_DATA_TYPE dtype);
93+
void *data_ptr, const int64_t *shape, int64_t rank, OM_DATA_TYPE dtype);
9494

9595
/**
9696
* \brief Create an OMTensor with specified data pointer, shape, rank and
@@ -121,7 +121,7 @@ OM_EXTERNAL_VISIBILITY OMTensor *omTensorCreate(
121121
*
122122
*/
123123
OM_EXTERNAL_VISIBILITY OMTensor *omTensorCreateWithOwnership(void *data_ptr,
124-
int64_t *shape, int64_t rank, OM_DATA_TYPE dtype, int64_t owning);
124+
const int64_t *shape, int64_t rank, OM_DATA_TYPE dtype, int64_t owning);
125125

126126
/**
127127
* Create an OMTensor with the specified shape, rank and element type,
@@ -141,7 +141,7 @@ OM_EXTERNAL_VISIBILITY OMTensor *omTensorCreateWithOwnership(void *data_ptr,
141141
*
142142
*/
143143
OM_EXTERNAL_VISIBILITY OMTensor *omTensorCreateEmpty(
144-
int64_t *shape, int64_t rank, OM_DATA_TYPE dtype);
144+
const int64_t *shape, int64_t rank, OM_DATA_TYPE dtype);
145145

146146
/**
147147
* \brief Destroy the OMTensor struct.
@@ -179,7 +179,7 @@ OM_EXTERNAL_VISIBILITY void *omTensorGetDataPtr(const OMTensor *tensor);
179179
* @param tensor pointer to the OMTensor
180180
* @return pointer to the data shape array.
181181
*/
182-
OM_EXTERNAL_VISIBILITY int64_t *omTensorGetShape(const OMTensor *tensor);
182+
OM_EXTERNAL_VISIBILITY const int64_t *omTensorGetShape(const OMTensor *tensor);
183183

184184
/**
185185
* \brief OMTensor data shape setter.
@@ -195,7 +195,7 @@ OM_EXTERNAL_VISIBILITY int64_t *omTensorGetShape(const OMTensor *tensor);
195195
*
196196
* Set the data shape array of the OMTensor to the values in the input array.
197197
*/
198-
OM_EXTERNAL_VISIBILITY void omTensorSetShape(OMTensor *tensor, int64_t *shape);
198+
OM_EXTERNAL_VISIBILITY void omTensorSetShape(OMTensor *tensor, const int64_t *shape);
199199

200200
/**
201201
* \brief OMTensor data strides getter
@@ -209,7 +209,7 @@ OM_EXTERNAL_VISIBILITY void omTensorSetShape(OMTensor *tensor, int64_t *shape);
209209
* @param tensor pointer to the OMTensor
210210
* @return pointer to the data strides array.
211211
*/
212-
OM_EXTERNAL_VISIBILITY int64_t *omTensorGetStrides(const OMTensor *tensor);
212+
OM_EXTERNAL_VISIBILITY const int64_t *omTensorGetStrides(const OMTensor *tensor);
213213

214214
/**
215215
* \brief OMTensor data strides setter
@@ -226,7 +226,7 @@ OM_EXTERNAL_VISIBILITY int64_t *omTensorGetStrides(const OMTensor *tensor);
226226
* Set the data strides array of the OMTensor to the values in the input array.
227227
*/
228228
OM_EXTERNAL_VISIBILITY void omTensorSetStrides(
229-
OMTensor *tensor, int64_t *stride);
229+
OMTensor *tensor, const int64_t *stride);
230230

231231
/**
232232
* \brief OMTensor data strides setter with stride values from PyArray strides
@@ -247,7 +247,7 @@ OM_EXTERNAL_VISIBILITY void omTensorSetStrides(
247247
* Set the data strides array of the OMTensor to the values in the input array.
248248
*/
249249
OM_EXTERNAL_VISIBILITY void omTensorSetStridesWithPyArrayStrides(
250-
OMTensor *tensor, int64_t *stridesInBytes);
250+
OMTensor *tensor, const int64_t *stridesInBytes);
251251

252252
/**
253253
* \brief OMTensor data type getter

include/onnx-mlir/Runtime/OMTensorList.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ OM_EXTERNAL_VISIBILITY void omTensorListDestroyShallow(OMTensorList *list);
7777
* @param list pointer to the OMTensorList
7878
* @return pointer to the array of OMTensor pointers.
7979
*/
80-
OM_EXTERNAL_VISIBILITY OMTensor **omTensorListGetOmtArray(OMTensorList *list);
80+
OM_EXTERNAL_VISIBILITY OMTensor **omTensorListGetOmtArray(const OMTensorList *list);
8181

8282
/**
8383
* \brief OMTensorList size getter
@@ -86,7 +86,7 @@ OM_EXTERNAL_VISIBILITY OMTensor **omTensorListGetOmtArray(OMTensorList *list);
8686
* @param list pointer to the OMTensorList
8787
* @return number of elements in the OMTensor array.
8888
*/
89-
OM_EXTERNAL_VISIBILITY int64_t omTensorListGetSize(OMTensorList *list);
89+
OM_EXTERNAL_VISIBILITY int64_t omTensorListGetSize(const OMTensorList *list);
9090

9191
/**
9292
* \brief OMTensorList OMTensor getter by index
@@ -96,7 +96,7 @@ OM_EXTERNAL_VISIBILITY int64_t omTensorListGetSize(OMTensorList *list);
9696
* @return pointer to the OMTensor, NULL if not found.
9797
*/
9898
OM_EXTERNAL_VISIBILITY OMTensor *omTensorListGetOmtByIndex(
99-
OMTensorList *list, int64_t index);
99+
const OMTensorList *list, int64_t index);
100100

101101
#ifdef __cplusplus
102102
}

src/Runtime/ExecutionSession.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
//===------- ExecutionSession.cpp - ExecutionSession Implementation -------===//
66
//
7-
// Copyright 2019-2020 The IBM Research Authors.
7+
// Copyright 2019-2023 The IBM Research Authors.
88
//
99
// =============================================================================
1010
//
@@ -83,7 +83,7 @@ std::vector<OMTensorUniquePtr> ExecutionSession::run(
8383
std::vector<OMTensor *> omts;
8484
for (const auto &inOmt : ins)
8585
omts.emplace_back(inOmt.get());
86-
auto *wrappedInput = omTensorListCreate(&omts[0], (int64_t)omts.size());
86+
auto *wrappedInput = omTensorListCreate(omts.data(), (int64_t)omts.size());
8787

8888
auto *wrappedOutput = _entryPointFunc(wrappedInput);
8989

src/Runtime/OMResize.inc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*/
4+
5+
//===---------- OMResize.cpp - OMTensor C/C++ Implementation ----------===//
6+
//
7+
// Copyright 2022-2023 The IBM Research Authors.
8+
//
9+
// =============================================================================
10+
//
11+
// This file contains C/C++ neutral implementation of OMTensorList data
12+
// structures and helper functions.
13+
//
14+
//===----------------------------------------------------------------------===//
115
#ifdef __cplusplus
216
#include <cassert>
317
#else
@@ -230,7 +244,7 @@ static void interpolate_nd_OMTensor(OMTensor *output_OMT, OMTensor *data,
230244
"Resize runtime: only float type is supported currently");
231245

232246
int64_t rank = omTensorGetRank(data);
233-
int64_t *inputShape = omTensorGetShape(data);
247+
const int64_t *inputShape = omTensorGetShape(data);
234248
float *scale_factor = NULL;
235249
int64_t *output_size = NULL;
236250
if (scale_factor_OMT != NULL)

src/Runtime/OMTensor.inc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
//===--------- OMTensor.inc - C/C++ Neutral OMTensor Implementation--------===//
66
//
7-
// Copyright 2019-2022 The IBM Research Authors.
7+
// Copyright 2019-2023 The IBM Research Authors.
88
//
99
// =============================================================================
1010
//
@@ -191,7 +191,7 @@ static inline int64_t computeElemOffset(
191191

192192
// Create a OMTensor.
193193
OMTensor *omTensorCreate(
194-
void *data_ptr, int64_t *shape, int64_t rank, OM_DATA_TYPE dtype) {
194+
void *data_ptr, const int64_t *shape, int64_t rank, OM_DATA_TYPE dtype) {
195195
OMTensor *tensor = (OMTensor *)malloc(sizeof(OMTensor));
196196
if (!tensor)
197197
return NULL;
@@ -221,7 +221,7 @@ OMTensor *omTensorCreate(
221221
}
222222

223223
// Create a OMTensor with specified ownership.
224-
OMTensor *omTensorCreateWithOwnership(void *data_ptr, int64_t *shape,
224+
OMTensor *omTensorCreateWithOwnership(void *data_ptr, const int64_t *shape,
225225
int64_t rank, OM_DATA_TYPE dtype, int64_t owning) {
226226
OMTensor *tensor = omTensorCreate(data_ptr, shape, rank, dtype);
227227
// If ctor fails, return NULL.
@@ -267,7 +267,7 @@ OMTensor *omTensorCreateUntyped(int64_t rank) {
267267
}
268268

269269
OMTensor *omTensorCreateEmpty(
270-
int64_t *shape, int64_t rank, OM_DATA_TYPE dtype) {
270+
const int64_t *shape, int64_t rank, OM_DATA_TYPE dtype) {
271271
OMTensor *tensor;
272272
void *dataPtr;
273273
if (((dataPtr = malloc(getNumElems(shape, rank) * getDataTypeSize(dtype))) ==
@@ -323,26 +323,26 @@ void omTensorSetDataPtr(
323323
}
324324

325325
/* OMTensor data shape getter */
326-
int64_t *omTensorGetShape(const OMTensor *tensor) { return tensor->_shape; }
326+
const int64_t *omTensorGetShape(const OMTensor *tensor) { return tensor->_shape; }
327327

328328
/* OMTensor data shape setter */
329-
void omTensorSetShape(OMTensor *tensor, int64_t *shape) {
329+
void omTensorSetShape(OMTensor *tensor, const int64_t *shape) {
330330
for (int64_t i = 0; i < tensor->_rank; i++)
331331
tensor->_shape[i] = shape[i];
332332
}
333333

334334
/* OMTensor data strides getter */
335-
int64_t *omTensorGetStrides(const OMTensor *tensor) { return tensor->_strides; }
335+
const int64_t *omTensorGetStrides(const OMTensor *tensor) { return tensor->_strides; }
336336

337337
/* OMTensor data strides setter */
338-
void omTensorSetStrides(OMTensor *tensor, int64_t *strides) {
338+
void omTensorSetStrides(OMTensor *tensor, const int64_t *strides) {
339339
for (int64_t i = 0; i < tensor->_rank; i++)
340340
tensor->_strides[i] = strides[i];
341341
}
342342

343343
/* OMTensor data strides setter with PyArray stride values */
344344
void omTensorSetStridesWithPyArrayStrides(
345-
OMTensor *tensor, int64_t *stridesInBytes) {
345+
OMTensor *tensor, const int64_t *stridesInBytes) {
346346
for (int64_t i = 0; i < tensor->_rank; i++)
347347
tensor->_strides[i] =
348348
stridesInBytes[i] / getDataTypeSize(tensor->_dataType);

src/Runtime/OMTensorList.inc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ void omTensorListDestroyShallow(OMTensorList *list) {
109109
}
110110

111111
/* OMTensorList OMTensor array getter */
112-
OMTensor **omTensorListGetOmtArray(OMTensorList *list) { return list->_omts; }
112+
OMTensor **omTensorListGetOmtArray(const OMTensorList *list) { return list->_omts; }
113113

114114
/* OMTensorList number of OMTensor getter */
115-
int64_t omTensorListGetSize(OMTensorList *list) { return list->_size; }
115+
int64_t omTensorListGetSize(const OMTensorList *list) { return list->_size; }
116116

117117
/* Return OMTensor at specified index in the OMTensorList */
118-
OMTensor *omTensorListGetOmtByIndex(OMTensorList *rlist, int64_t index) {
118+
OMTensor *omTensorListGetOmtByIndex(const OMTensorList *rlist, int64_t index) {
119119
assert(index >= 0);
120120
assert(index < rlist->_size);
121121
return rlist->_omts[index];

src/Runtime/jni/jniwrapper.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -562,12 +562,12 @@ jobject omtl_native_to_java(
562562

563563
LIB_TYPE_VAR_CALL(void *, jni_data, omTensorGetDataPtr(jni_omts[i]),
564564
jni_data != NULL, env, japi->jecpt_cls, "omt[%d]:data=%p", i, jni_data);
565-
LIB_TYPE_VAR_CALL(int64_t *, jni_shape, omTensorGetShape(jni_omts[i]),
565+
LIB_TYPE_VAR_CALL(const int64_t *, jni_shape, omTensorGetShape(jni_omts[i]),
566566
jni_shape != NULL, env, japi->jecpt_cls, "omt[%d]:shape=%p", i,
567567
jni_shape);
568-
LIB_TYPE_VAR_CALL(int64_t *, jni_strides, omTensorGetStrides(jni_omts[i]),
569-
jni_strides != NULL, env, japi->jecpt_cls, "omt[%d]:strides=%p", i,
570-
jni_strides);
568+
LIB_TYPE_VAR_CALL(const int64_t *, jni_strides,
569+
omTensorGetStrides(jni_omts[i]), jni_strides != NULL, env,
570+
japi->jecpt_cls, "omt[%d]:strides=%p", i, jni_strides);
571571
LIB_TYPE_VAR_CALL(OM_DATA_TYPE, jni_dataType,
572572
omTensorGetDataType(jni_omts[i]), jni_dataType != ONNX_TYPE_UNDEFINED,
573573
env, japi->jecpt_cls, "omt[%d]:dataType=%d", i, jni_dataType);

test/modellib/ModelLib.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
//===========-- ModelLib.cpp - Helper function for building models -==========//
66
//
7-
// Copyright 2022 The IBM Research Authors.
7+
// Copyright 2022-2023 The IBM Research Authors.
88
//
99
// =============================================================================
1010
//
@@ -208,7 +208,7 @@ void ModelLibBuilder::printIndices(
208208
void ModelLibBuilder::printTensor(
209209
const OMTensor *t, std::vector<int64_t> &indices, bool isLast) const {
210210
int64_t rank = omTensorGetRank(t);
211-
int64_t *shape = omTensorGetShape(t);
211+
const int64_t *shape = omTensorGetShape(t);
212212
int64_t currSize = indices.size();
213213
// Utility to print tabs.
214214
auto printTab = [](int currSize) {
@@ -253,7 +253,7 @@ void ModelLibBuilder::printTensor(
253253
void ModelLibBuilder::printTensor(
254254
const std::string varName, const OMTensor *t, bool asNumpy) const {
255255
int64_t rank = omTensorGetRank(t);
256-
int64_t *shape = omTensorGetShape(t);
256+
const int64_t *shape = omTensorGetShape(t);
257257
std::vector<int64_t> shapeVect(shape, shape + rank);
258258
// Print message as comment and add rank and shape.
259259
printf("# ");

test/numerical/TestGemm.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
//====-- TestGemm.cpp - test GEMM code -======================================//
66
//
7-
// Copyright 2022 The IBM Research Authors.
7+
// Copyright 2022-2023 The IBM Research Authors.
88
//
99
// =============================================================================
1010
//
@@ -29,7 +29,7 @@ void *omTensorGetAllocatedPtr(OMTensor *tensor);
2929
template <typename TYPE>
3030
void omPrintAsPython(OMTensor *tensor, std::string name) {
3131
int rank = omTensorGetRank(tensor);
32-
int64_t *shape = omTensorGetShape(tensor);
32+
const int64_t *shape = omTensorGetShape(tensor);
3333
if (false) {
3434
printf("# tensor 0x%llx, allocated addr 0x%llx, data addr 0x%llx\n",
3535
(long long)tensor, (long long)omTensorGetAllocatedPtr(tensor),

test/numerical/TestScan.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
//====-- TestScan.cpp - test Scan code -======================================//
66
//
7-
// Copyright 2022 The IBM Research Authors.
7+
// Copyright 2022-2023 The IBM Research Authors.
88
//
99
// =============================================================================
1010
//
@@ -29,7 +29,7 @@ void *omTensorGetAllocatedPtr(OMTensor *tensor);
2929
template <typename TYPE>
3030
void omPrintAsPython(OMTensor *tensor, std::string name) {
3131
int rank = omTensorGetRank(tensor);
32-
int64_t *shape = omTensorGetShape(tensor);
32+
const int64_t *shape = omTensorGetShape(tensor);
3333
if (false) {
3434
printf("# tensor 0x%llx, allocated addr 0x%llx, data addr 0x%llx\n",
3535
(long long)tensor, (long long)omTensorGetAllocatedPtr(tensor),

0 commit comments

Comments
 (0)