diff --git a/clang/include/clang/Basic/Targets/SPIR.h b/clang/include/clang/Basic/Targets/SPIR.h new file mode 100644 index 0000000000000..95ffda3b8f230 --- /dev/null +++ b/clang/include/clang/Basic/Targets/SPIR.h @@ -0,0 +1,19 @@ +//===---- SPIR.h - Declare SPIR and SPIR-V target interfaces ----*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#pragma once + +namespace clang { +namespace targets { + +// Used by both the SPIR and SPIR-V targets. Code of the generic address space +// for the target +constexpr unsigned SPIR_GENERIC_AS = 4u; + +} // namespace targets +} // namespace clang diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 3908831bb2732..77a60c0678f96 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -11,6 +11,7 @@ #include "clang/Basic/Diagnostic.h" #include "clang/Basic/LangOptions.h" #include "clang/Basic/TargetOptions.h" +#include "clang/Basic/Targets/SPIR.h" #include "clang/Frontend/FrontendDiagnostic.h" #include "clang/Frontend/Utils.h" #include "clang/Lex/HeaderSearchOptions.h" @@ -86,6 +87,7 @@ #include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Scalar/EarlyCSE.h" #include "llvm/Transforms/Scalar/GVN.h" +#include "llvm/Transforms/Scalar/InferAddressSpaces.h" #include "llvm/Transforms/Scalar/JumpThreading.h" #include "llvm/Transforms/Scalar/LowerMatrixIntrinsics.h" #include "llvm/Transforms/Scalar/NewGVN.h" @@ -894,6 +896,15 @@ void EmitAssemblyHelper::RunOptimizationPipeline( MPM.addPass(SYCLPropagateAspectsUsagePass()); }); + // Add the InferAddressSpaces pass for all the SPIR[V] targets + if (TargetTriple.isSPIR() || TargetTriple.isSPIRV()) { + PB.registerOptimizerLastEPCallback( + [](ModulePassManager &MPM, OptimizationLevel Level) { + MPM.addPass(createModuleToFunctionPassAdaptor( + InferAddressSpacesPass(clang::targets::SPIR_GENERIC_AS))); + }); + } + bool IsThinLTO = CodeGenOpts.PrepareForThinLTO; bool IsLTO = CodeGenOpts.PrepareForLTO; @@ -996,7 +1007,7 @@ void EmitAssemblyHelper::RunOptimizationPipeline( // -fsycl-instrument-device-code option was passed. This option can be used // only with spir triple. if (LangOpts.SYCLIsDevice && CodeGenOpts.SPIRITTAnnotations) { - assert(llvm::Triple(TheModule->getTargetTriple()).isSPIR() && + assert(TargetTriple.isSPIR() && "ITT annotations can only be added to a module with spir target"); MPM.addPass(SPIRITTAnnotationsPass()); } diff --git a/clang/test/CodeGenSYCL/infer-address-spaces.cpp b/clang/test/CodeGenSYCL/infer-address-spaces.cpp new file mode 100644 index 0000000000000..8828462fc8433 --- /dev/null +++ b/clang/test/CodeGenSYCL/infer-address-spaces.cpp @@ -0,0 +1,21 @@ +// RUN: %clang_cc1 -O1 -fsycl-is-device -internal-isystem %S/Inputs -triple spir64 -emit-llvm %s -o - | FileCheck %s + +// Test that address spaces are deduced correctly by compiler optimizations. + +#include "sycl.hpp" + +using namespace sycl; + +void foo(const float *usm_in, float* usm_out) { + queue Q; + Q.submit([&](handler &cgh) { + cgh.single_task([=](){ + *usm_out = *usm_in; + }); + }); +} + +// No addrspacecast before loading and storing values +// CHECK-NOT: addrspacecast +// CHECK: %0 = load float, ptr addrspace(1) +// CHECK: store float %0, ptr addrspace(1)