From f44f6c5b2e2e4eae7e637b24d118d770f5902a09 Mon Sep 17 00:00:00 2001 From: Numfor Tiapo Date: Tue, 25 Apr 2023 16:41:39 -0700 Subject: [PATCH] Fix Prefast Errors (#15651) This PR adds fixes for prefast errors with the following codes: - C26814 - C26451 - C26400 --- .../dml/DmlExecutionProvider/src/ExecutionProvider.cpp | 2 +- .../src/External/DirectMLHelpers/DirectMLX.h | 2 +- .../src/Operators/DmlOperatorActivation.cpp | 2 +- .../DmlExecutionProvider/src/Operators/DmlOperatorEinSum.cpp | 3 ++- .../DmlExecutionProvider/src/Operators/DmlOperatorResize.cpp | 2 +- .../providers/dml/DmlExecutionProvider/src/TensorDesc.cpp | 2 +- .../providers/dml/OperatorAuthorHelper/OperatorHelper.cpp | 2 +- winml/lib/Api.Image/CpuDetensorizer.h | 4 ++-- winml/lib/Api.Image/CpuTensorizer.h | 2 +- winml/lib/Api.Image/TensorToVideoFrameConverter.cpp | 2 +- winml/lib/Api.Image/VideoFrameToTensorConverter.cpp | 2 +- winml/lib/Api.Ort/OnnxruntimeEngine.cpp | 4 ++-- winml/test/image/imagetests.cpp | 2 +- winml/test/model/ort_value_helper.cpp | 4 ++-- winml/test/scenario/cppwinrt/ReluCpu.h | 2 +- 15 files changed, 19 insertions(+), 18 deletions(-) diff --git a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/ExecutionProvider.cpp b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/ExecutionProvider.cpp index 90ecc7f87f..a1f3219d7c 100644 --- a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/ExecutionProvider.cpp +++ b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/ExecutionProvider.cpp @@ -443,7 +443,7 @@ namespace Dml ID3D12Resource* dstData = dstAllocInfo->GetResource(); const void* srcData = src->GetData(); - const uint64_t dstOffset = 0; + constexpr uint64_t dstOffset = 0; const auto dstState = D3D12_RESOURCE_STATE_UNORDERED_ACCESS; // GPU resources are always kept in UAV state m_uploadHeap->BeginUploadToGpu(dstData, dstOffset, dstState, AsByteSpan(srcData, dataSizeInBytes)); diff --git a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/External/DirectMLHelpers/DirectMLX.h b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/External/DirectMLHelpers/DirectMLX.h index 3f56ad5a70..f7feb5ff21 100644 --- a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/External/DirectMLHelpers/DirectMLX.h +++ b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/External/DirectMLHelpers/DirectMLX.h @@ -97,7 +97,7 @@ inline UINT64 DMLCalcBufferTensorSize( } else { - UINT indexOfLastElement = 0; + UINT64 indexOfLastElement = 0; for (UINT i = 0; i < dimensionCount; ++i) { indexOfLastElement += (sizes[i] - 1) * strides[i]; diff --git a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorActivation.cpp b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorActivation.cpp index 10c41ad10d..339f912856 100644 --- a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorActivation.cpp +++ b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorActivation.cpp @@ -40,7 +40,7 @@ public: { const uint32_t onnxDimCount = gsl::narrow_cast(kernelCreationContext.GetTensorShapeDescription().GetInputTensorShape(0).size()); int axis = HandleNegativeAxis(kernelCreationContext.GetOptionalAttribute(AttrName::Axis, 1), onnxDimCount); - std::vector onnxAxes(onnxDimCount - axis); + std::vector onnxAxes(static_cast(onnxDimCount) - axis); std::iota(onnxAxes.begin(), onnxAxes.end(), static_cast(axis)); dmlAxes.resize(onnxDimCount - axis); diff --git a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorEinSum.cpp b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorEinSum.cpp index fca2efaf30..c8217e4343 100644 --- a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorEinSum.cpp +++ b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorEinSum.cpp @@ -13,7 +13,8 @@ public: : DmlOperator(kernelCreationContext), EinSumHelper(kernelCreationContext, kernelCreationContext.GetTensorShapeDescription(), opsetVersion) { - ML_CHECK_VALID_ARGUMENT(kernelCreationContext.GetInputCount() + 1 == m_components.size(), "EinSum input tensor count is inconsistent with the equation component count."); + ML_CHECK_VALID_ARGUMENT(static_cast(kernelCreationContext.GetInputCount()) + 1 == m_components.size(), + "EinSum input tensor count is inconsistent with the equation component count."); ML_CHECK_VALID_ARGUMENT(kernelCreationContext.GetOutputCount() == 1, "EinSum expects one output tensor."); std::vector> inputIndices = {0,1,2}; diff --git a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorResize.cpp b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorResize.cpp index 68931a9317..f332fac9d3 100644 --- a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorResize.cpp +++ b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorResize.cpp @@ -59,7 +59,7 @@ void ComputePixelOffsetsAndScales( // Fill in all the input/output pixel offset for each axis, // and recompute the scale for certain modes. - for (uint32_t i = 0; i < rank; ++i) + for (uint64_t i = 0; i < rank; ++i) { float inputPixelOffset = 0; float outputPixelOffset = 0; diff --git a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/TensorDesc.cpp b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/TensorDesc.cpp index 625d44e138..067a320dd8 100644 --- a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/TensorDesc.cpp +++ b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/TensorDesc.cpp @@ -79,7 +79,7 @@ TensorDesc::TensorDesc( dimension0 *= dimensions[i]; } - for (size_t i = coerceAxis + 1, ci = dimensions.size(); i < ci; ++i) + for (size_t i = static_cast(coerceAxis) + 1, ci = dimensions.size(); i < ci; ++i) { dimension1 *= dimensions[i]; } diff --git a/onnxruntime/core/providers/dml/OperatorAuthorHelper/OperatorHelper.cpp b/onnxruntime/core/providers/dml/OperatorAuthorHelper/OperatorHelper.cpp index 34878d0b5f..1ea466b771 100644 --- a/onnxruntime/core/providers/dml/OperatorAuthorHelper/OperatorHelper.cpp +++ b/onnxruntime/core/providers/dml/OperatorAuthorHelper/OperatorHelper.cpp @@ -440,7 +440,7 @@ namespace OperatorHelper // if pads are not specified, assume all pad values are 0 if (pads.empty()) { - pads.resize(2 * spatialDimensionCount); + pads.resize(2 * static_cast(spatialDimensionCount)); } ML_CHECK_VALID_ARGUMENT(pads.size() >= 2 * spatialDimensionCount); diff --git a/winml/lib/Api.Image/CpuDetensorizer.h b/winml/lib/Api.Image/CpuDetensorizer.h index 63aab88e80..dc5e76ef0d 100644 --- a/winml/lib/Api.Image/CpuDetensorizer.h +++ b/winml/lib/Api.Image/CpuDetensorizer.h @@ -227,7 +227,7 @@ class CpuDetensorizer { xChannel += 8; yChannel += 8; zChannel += 8; - pPixel += 8 * bytesPerPixel; + pPixel += 8 * static_cast(bytesPerPixel); tensorWidthRemaining -= 8; } @@ -238,7 +238,7 @@ class CpuDetensorizer { pPixel[2] = DetensorizeValue(zChannel, nominalRangeConverter); pPixel[3] = 255; - pPixel += bytesPerPixel; + pPixel += static_cast(bytesPerPixel); xChannel++; yChannel++; zChannel++; diff --git a/winml/lib/Api.Image/CpuTensorizer.h b/winml/lib/Api.Image/CpuTensorizer.h index a24c06d5c3..27a7f2f486 100644 --- a/winml/lib/Api.Image/CpuTensorizer.h +++ b/winml/lib/Api.Image/CpuTensorizer.h @@ -39,7 +39,7 @@ class CpuTensorizer { if (formatFrom == kImageTensorChannelTypeBGR8 && formatTo == kImageTensorChannelTypeBGR8 || formatFrom == kImageTensorChannelTypeRGB8 && formatTo == kImageTensorChannelTypeRGB8) { // Convert BGR8 -> BGR8 or RGB8 -> RGB8 - for (uint32_t y = 0; y < yElements; y++) { + for (uint64_t y = 0; y < yElements; y++) { DeinterleaveRowByteToFloat( pBuffer + y * bufferWidth + start, pCPUTensor + y * inputBounds.Width, diff --git a/winml/lib/Api.Image/TensorToVideoFrameConverter.cpp b/winml/lib/Api.Image/TensorToVideoFrameConverter.cpp index b5d0becf63..8431bfa7f0 100644 --- a/winml/lib/Api.Image/TensorToVideoFrameConverter.cpp +++ b/winml/lib/Api.Image/TensorToVideoFrameConverter.cpp @@ -579,7 +579,7 @@ void TensorToVideoFrameConverter::ConvertGPUTensorToSoftwareBitmap( auto barrier = CD3DX12_RESOURCE_BARRIER::Transition(pInputTensor, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE); command_list_->ResourceBarrier(1, &barrier); - command_list_->CopyBufferRegion(readback_heap_.Get(), 0, pInputTensor, singleVideoFramebufferSize * batchIdx, singleVideoFramebufferSize); + command_list_->CopyBufferRegion(readback_heap_.Get(), 0, pInputTensor, static_cast(singleVideoFramebufferSize) * batchIdx, singleVideoFramebufferSize); WINML_THROW_IF_FAILED(command_list_->Close()); ID3D12CommandList* ppCommandLists[] = {command_list_.Get()}; diff --git a/winml/lib/Api.Image/VideoFrameToTensorConverter.cpp b/winml/lib/Api.Image/VideoFrameToTensorConverter.cpp index c4490db394..19637018ee 100644 --- a/winml/lib/Api.Image/VideoFrameToTensorConverter.cpp +++ b/winml/lib/Api.Image/VideoFrameToTensorConverter.cpp @@ -446,7 +446,7 @@ void VideoFrameToTensorConverter::ConvertDX12TextureToGPUTensor( command_list_->SetComputeRootDescriptorTable(2, uavHandle); UINT64 dispatchWidth = (inputDesc.Width - 1) / 16 + 1; - UINT64 dispatchHeight = (inputDesc.Height - 1) / 4 + 1; + UINT64 dispatchHeight = (static_cast(inputDesc.Height) - 1) / 4 + 1; command_list_->Dispatch(static_cast(dispatchWidth), static_cast(dispatchHeight), 1); WINML_THROW_IF_FAILED(command_list_->Close()); diff --git a/winml/lib/Api.Ort/OnnxruntimeEngine.cpp b/winml/lib/Api.Ort/OnnxruntimeEngine.cpp index ee45ceac94..b492135de9 100644 --- a/winml/lib/Api.Ort/OnnxruntimeEngine.cpp +++ b/winml/lib/Api.Ort/OnnxruntimeEngine.cpp @@ -110,7 +110,7 @@ HRESULT OnnxruntimeValue::IsCpu(bool* out) { return S_OK; } -static int64_t ShapeSize(const int64_t* shape, size_t count) { +static uint64_t ShapeSize(const int64_t* shape, size_t count) { // for each dim int64_t size = 1; for (size_t i = 0; i < count; i++) { @@ -151,7 +151,7 @@ static auto GetStrings(const OrtApi* ort_api, const OrtValue* ort_value, ort_api); // now go build all the strings - for (auto i = 0; i < length; ++i) { + for (size_t i = 0; i < length; ++i) { size_t str_len = 0; // are we on the last one? if (i == (length - 1)) { diff --git a/winml/test/image/imagetests.cpp b/winml/test/image/imagetests.cpp index 7a76e84958..73b9e381c1 100644 --- a/winml/test/image/imagetests.cpp +++ b/winml/test/image/imagetests.cpp @@ -835,7 +835,7 @@ TEST_F(ImageTests, ImageBindingAsGPUTensor) { IRandomAccessStream stream = image_file.OpenAsync(FileAccessMode::Read).get(); SoftwareBitmap software_bitmap = (BitmapDecoder::CreateAsync(stream).get()).GetSoftwareBitmapAsync().get(); - UINT64 buffer_byte_size = software_bitmap.PixelWidth()*software_bitmap.PixelHeight() * 3 * sizeof(float); + UINT64 buffer_byte_size = static_cast(software_bitmap.PixelWidth()) * software_bitmap.PixelHeight() * 3 * sizeof(float); D3D12_HEAP_PROPERTIES heap_properties = { D3D12_HEAP_TYPE_DEFAULT, D3D12_CPU_PAGE_PROPERTY_UNKNOWN, diff --git a/winml/test/model/ort_value_helper.cpp b/winml/test/model/ort_value_helper.cpp index 45749a1642..3a2de24309 100644 --- a/winml/test/model/ort_value_helper.cpp +++ b/winml/test/model/ort_value_helper.cpp @@ -14,7 +14,7 @@ winml::ITensor CreateTensorFromShape(std::vector& shape) return tensor; } -static int64_t ShapeSize(const int64_t* shape, size_t count) { +static uint64_t ShapeSize(const int64_t* shape, size_t count) { // for each dim int64_t size = 1; for (size_t i = 0; i < count; i++) { @@ -46,7 +46,7 @@ winml::ITensor CreateStringTensor(Ort::Value& val) { WINML_EXPECT_NO_THROW(val.GetStringTensorContent(buffer.get(), bufferLength, offsets.data(), offsets.size())); // now go build all the strings - for (auto i = 0; i < length; ++i) { + for (size_t i = 0; i < length; ++i) { size_t strLength = 0; // are we on the last one? if (i == (length - 1)) { diff --git a/winml/test/scenario/cppwinrt/ReluCpu.h b/winml/test/scenario/cppwinrt/ReluCpu.h index 44b59a5f1d..778aa0362c 100644 --- a/winml/test/scenario/cppwinrt/ReluCpu.h +++ b/winml/test/scenario/cppwinrt/ReluCpu.h @@ -13,7 +13,7 @@ struct ReluShapeInferrer : winrt::implementsGetInputTensorDimensionCount(0, &inputDimsSize); - uint32_t *inputDims = new uint32_t[inputDimsSize]; + auto inputDims = new uint32_t[inputDimsSize]; context->GetInputTensorShape(0, inputDimsSize, inputDims); context->SetOutputTensorShape(0, inputDimsSize, inputDims);