[flang-commits] [flang] [flang] Updating drivers to create data layout before semantics (PR #73301)
Andrzej WarzyĆski via flang-commits
flang-commits at lists.llvm.org
Sun Nov 26 12:34:08 PST 2023
================
@@ -0,0 +1,63 @@
+//===-- Tools/TargetSetup.h ------------------------------------- *-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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef FORTRAN_TOOLS_TARGET_SETUP_H
+#define FORTRAN_TOOLS_TARGET_SETUP_H
+
+#include "flang/Evaluate/target.h"
+#include "llvm/MC/TargetRegistry.h"
+#include "llvm/Target/TargetMachine.h"
+#include "llvm/TargetParser/Host.h"
+#include <memory>
+
+namespace Fortran::tools {
+
+[[maybe_unused]] inline static void setUpTargetCharacteristics(
+ Fortran::evaluate::TargetCharacteristics &targetCharacteristics,
+ const llvm::TargetMachine &targetMachine,
+ const std::string &compilerVersion, const std::string &compilerOptions) {
+
+ const llvm::Triple &targetTriple{targetMachine.getTargetTriple()};
+ // FIXME: Handle real(3) ?
+ if (targetTriple.getArch() != llvm::Triple::ArchType::x86_64)
+ targetCharacteristics.DisableType(
+ Fortran::common::TypeCategory::Real, /*kind=*/10);
+
+ targetCharacteristics.set_compilerOptionsString(compilerOptions)
+ .set_compilerVersionString(compilerVersion);
+
+ if (targetTriple.isPPC())
+ targetCharacteristics.set_isPPC(true);
+
+ // TODO: use target machine data layout to set-up the target characteristics
+ // type size and alignment info.
+}
+
+/// Create a target machine that is at least sufficient to get data-layout
+/// information required by flang semantics and lowering. Note that it may not
+/// contain all the CPU feature information to get optimized assembly generation
+/// from LLVM IR. Drivers that needs to generate assembly from LLVM IR should
+/// create a target machine according to their specific options.
+[[maybe_unused]] inline static std::unique_ptr<llvm::TargetMachine>
+createTargetMachine(llvm::StringRef targetTriple, std::string &error) {
+ std::string triple{targetTriple};
+ if (triple.empty())
+ triple = llvm::sys::getDefaultTargetTriple();
+
+ const llvm::Target *theTarget =
+ llvm::TargetRegistry::lookupTarget(triple, error);
+ if (!theTarget)
+ return nullptr;
+ return std::unique_ptr<llvm::TargetMachine>{
+ theTarget->createTargetMachine(triple, /*CPU=*/"",
+ /*Features=*/"", llvm::TargetOptions(),
+ /*Reloc::Model=*/std::nullopt)};
+}
----------------
banach-space wrote:
Given: https://github.com/llvm/llvm-project/blob/9ba74c2ee044381544c8e1e736c4c85a1a9d56bf/llvm/include/llvm/MC/TargetRegistry.h#L484-L500
this method shouldn't really be called `createTargetMachine` (to avoid confusion). If a specialisation of the original method is required, then IMHO it should live in LLVM.
Also, from what I can see this is only needed by `bbc`, so I suggest moving it there. "Tools" based on LLVM Flang can leverage the APIs exposed by the driver logic that will handle this sort of configuration automatically.
https://github.com/llvm/llvm-project/pull/73301
More information about the flang-commits
mailing list