xref: /llvm-project/mlir/lib/Target/LLVMIR/Dialect/OpenMPCommon.cpp (revision 095b41c6eedb3acc908dc63ee91ff77944c07d75)
1 //===- OpenMPCommon.cpp - Utils for translating MLIR dialect to LLVM IR----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines general utilities for MLIR Dialect translations to LLVM IR.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Target/LLVMIR/Dialect/OpenMPCommon.h"
14 
15 llvm::Constant *
16 mlir::LLVM::createSourceLocStrFromLocation(Location loc,
17                                            llvm::OpenMPIRBuilder &builder,
18                                            StringRef name, uint32_t &strLen) {
19   if (auto fileLoc = dyn_cast<FileLineColLoc>(loc)) {
20     StringRef fileName = fileLoc.getFilename();
21     unsigned lineNo = fileLoc.getLine();
22     unsigned colNo = fileLoc.getColumn();
23     return builder.getOrCreateSrcLocStr(name, fileName, lineNo, colNo, strLen);
24   }
25   std::string locStr;
26   llvm::raw_string_ostream locOS(locStr);
27   locOS << loc;
28   return builder.getOrCreateSrcLocStr(locStr, strLen);
29 }
30 
31 llvm::Constant *
32 mlir::LLVM::createMappingInformation(Location loc,
33                                      llvm::OpenMPIRBuilder &builder) {
34   uint32_t strLen;
35   if (auto nameLoc = dyn_cast<NameLoc>(loc)) {
36     StringRef name = nameLoc.getName();
37     return createSourceLocStrFromLocation(nameLoc.getChildLoc(), builder, name,
38                                           strLen);
39   }
40   return createSourceLocStrFromLocation(loc, builder, "unknown", strLen);
41 }
42