Skip to content

Commit 9f8619c

Browse files
author
Cameron McInally
committed
[flang] Add support for -mprefer-vector-width=<value>
This patch adds support for the -mprefer-vector-width=<value> command line option. The parsing of this options is equivalent to Clang's and it is implemented by setting the "prefer-vector-width" function attribute.
1 parent 294643e commit 9f8619c

File tree

12 files changed

+57
-2
lines changed

12 files changed

+57
-2
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5480,7 +5480,7 @@ def mrecip_EQ : CommaJoined<["-"], "mrecip=">, Group<m_Group>,
54805480
"<value> = ( ['!'] ['vec-'] ('rcp'|'sqrt') [('h'|'s'|'d')] [':'<n>] ) | 'all' | 'default' | 'none'">,
54815481
MarshallingInfoStringVector<CodeGenOpts<"Reciprocals">>;
54825482
def mprefer_vector_width_EQ : Joined<["-"], "mprefer-vector-width=">, Group<m_Group>,
5483-
Visibility<[ClangOption, CC1Option]>,
5483+
Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
54845484
HelpText<"Specifies preferred vector width for auto-vectorization. Defaults to 'none' which allows target specific decisions.">,
54855485
MarshallingInfoString<CodeGenOpts<"PreferVectorWidth">>;
54865486
def mstack_protector_guard_EQ : Joined<["-"], "mstack-protector-guard=">, Group<m_Group>,

flang/include/flang/Frontend/CodeGenOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class CodeGenOptions : public CodeGenOptionsBase {
5353
/// The paths to the pass plugins that were registered using -fpass-plugin.
5454
std::vector<std::string> LLVMPassPlugins;
5555

56+
// The prefered vector width, if requested by -mprefer-vector-width.
57+
std::string PreferVectorWidth;
58+
5659
/// List of filenames passed in using the -fembed-offload-object option. These
5760
/// are offloading binaries containing device images and metadata.
5861
std::vector<std::string> OffloadObjects;

flang/include/flang/Optimizer/Transforms/Passes.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,10 @@ def FunctionAttr : Pass<"function-attr", "mlir::func::FuncOp"> {
418418
"module.">,
419419
Option<"unsafeFPMath", "unsafe-fp-math", "bool", /*default=*/"false",
420420
"Set the unsafe-fp-math attribute on functions in the module.">,
421+
Option<"preferVectorWidth", "prefer-vector-width", "std::string",
422+
/*default=*/"",
423+
"Set the prefer-vector-width attribute on functions in the "
424+
"module.">,
421425
Option<"tuneCPU", "tune-cpu", "std::string", /*default=*/"",
422426
"Set the tune-cpu attribute on functions in the module.">,
423427
];

flang/include/flang/Tools/CrossToolHelpers.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ struct MLIRToLLVMPassPipelineConfig : public FlangEPCallBacks {
102102
UnsafeFPMath = mathOpts.getAssociativeMath() &&
103103
mathOpts.getReciprocalMath() && NoSignedZerosFPMath &&
104104
ApproxFuncFPMath && mathOpts.getFPContractEnabled();
105+
PreferVectorWidth = opts.PreferVectorWidth;
105106
if (opts.InstrumentFunctions) {
106107
InstrumentFunctionEntry = "__cyg_profile_func_enter";
107108
InstrumentFunctionExit = "__cyg_profile_func_exit";
@@ -126,6 +127,8 @@ struct MLIRToLLVMPassPipelineConfig : public FlangEPCallBacks {
126127
bool NoSignedZerosFPMath =
127128
false; ///< Set no-signed-zeros-fp-math attribute for functions.
128129
bool UnsafeFPMath = false; ///< Set unsafe-fp-math attribute for functions.
130+
std::string PreferVectorWidth = ""; ///< Set prefer-vector-width attribute for
131+
///< functions.
129132
bool NSWOnLoopVarInc = true; ///< Add nsw flag to loop variable increments.
130133
bool EnableOpenMP = false; ///< Enable OpenMP lowering.
131134
std::string InstrumentFunctionEntry =

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,20 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
309309
for (auto *a : args.filtered(clang::driver::options::OPT_fpass_plugin_EQ))
310310
opts.LLVMPassPlugins.push_back(a->getValue());
311311

312+
// -mprefer_vector_width option
313+
if (const llvm::opt::Arg *a = args.getLastArg(
314+
clang::driver::options::OPT_mprefer_vector_width_EQ)) {
315+
llvm::StringRef s = a->getValue();
316+
unsigned Width;
317+
if (s == "none")
318+
opts.PreferVectorWidth = "none";
319+
else if (s.getAsInteger(10, Width))
320+
diags.Report(clang::diag::err_drv_invalid_value)
321+
<< a->getAsString(args) << a->getValue();
322+
else
323+
opts.PreferVectorWidth = s.str();
324+
}
325+
312326
// -fembed-offload-object option
313327
for (auto *a :
314328
args.filtered(clang::driver::options::OPT_fembed_offload_object_EQ))

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,8 @@ void CodeGenAction::generateLLVMIR() {
741741
config.VScaleMax = vsr->second;
742742
}
743743

744+
config.PreferVectorWidth = opts.PreferVectorWidth;
745+
744746
if (ci.getInvocation().getFrontendOpts().features.IsEnabled(
745747
Fortran::common::LanguageFeature::OpenMP))
746748
config.EnableOpenMP = true;

flang/lib/Optimizer/Passes/Pipelines.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
354354
{framePointerKind, config.InstrumentFunctionEntry,
355355
config.InstrumentFunctionExit, config.NoInfsFPMath, config.NoNaNsFPMath,
356356
config.ApproxFuncFPMath, config.NoSignedZerosFPMath, config.UnsafeFPMath,
357-
""}));
357+
config.PreferVectorWidth, ""}));
358358

359359
if (config.EnableOpenMP) {
360360
pm.addNestedPass<mlir::func::FuncOp>(

flang/lib/Optimizer/Transforms/FunctionAttr.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class FunctionAttrPass : public fir::impl::FunctionAttrBase<FunctionAttrPass> {
3636
approxFuncFPMath = options.approxFuncFPMath;
3737
noSignedZerosFPMath = options.noSignedZerosFPMath;
3838
unsafeFPMath = options.unsafeFPMath;
39+
preferVectorWidth = options.preferVectorWidth;
3940
}
4041
FunctionAttrPass() {}
4142
void runOnOperation() override;
@@ -102,6 +103,10 @@ void FunctionAttrPass::runOnOperation() {
102103
func->setAttr(
103104
mlir::LLVM::LLVMFuncOp::getUnsafeFpMathAttrName(llvmFuncOpName),
104105
mlir::BoolAttr::get(context, true));
106+
if (!preferVectorWidth.empty())
107+
func->setAttr(
108+
mlir::LLVM::LLVMFuncOp::getPreferVectorWidthAttrName(llvmFuncOpName),
109+
mlir::StringAttr::get(context, preferVectorWidth));
105110

106111
LLVM_DEBUG(llvm::dbgs() << "=== End " DEBUG_TYPE " ===\n");
107112
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
! Test that -mprefer-vector-width works as expected.
2+
3+
! RUN: %flang_fc1 -emit-llvm -o - %s 2>&1| FileCheck %s --check-prefix=CHECK-DEF
4+
! RUN: %flang_fc1 -mprefer-vector-width=none -emit-llvm -o - %s 2>&1| FileCheck %s --check-prefix=CHECK-NONE
5+
! RUN: %flang_fc1 -mprefer-vector-width=128 -emit-llvm -o - %s 2>&1| FileCheck %s --check-prefix=CHECK-128
6+
! RUN: %flang_fc1 -mprefer-vector-width=256 -emit-llvm -o - %s 2>&1| FileCheck %s --check-prefix=CHECK-256
7+
! RUN: not %flang_fc1 -mprefer-vector-width=xxx -emit-llvm -o - %s 2>&1| FileCheck %s --check-prefix=CHECK-INVALID
8+
9+
subroutine func
10+
end subroutine func
11+
12+
! CHECK-DEF-NOT: attributes #0 = { "prefer-vector-width"={{.*}} }
13+
! CHECK-NONE: attributes #0 = { "prefer-vector-width"="none" }
14+
! CHECK-128: attributes #0 = { "prefer-vector-width"="128" }
15+
! CHECK-256: attributes #0 = { "prefer-vector-width"="256" }
16+
! CHECK-INVALID:error: invalid value 'xxx' in '-mprefer-vector-width=xxx'

mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,6 +1893,7 @@ def LLVM_LLVMFuncOp : LLVM_Op<"func", [
18931893
OptionalAttr<FramePointerKindAttr>:$frame_pointer,
18941894
OptionalAttr<StrAttr>:$target_cpu,
18951895
OptionalAttr<StrAttr>:$tune_cpu,
1896+
OptionalAttr<StrAttr>:$prefer_vector_width,
18961897
OptionalAttr<LLVM_TargetFeaturesAttr>:$target_features,
18971898
OptionalAttr<BoolAttr>:$unsafe_fp_math,
18981899
OptionalAttr<BoolAttr>:$no_infs_fp_math,

mlir/lib/Target/LLVMIR/ModuleImport.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2636,6 +2636,10 @@ void ModuleImport::processFunctionAttributes(llvm::Function *func,
26362636
funcOp.setTargetFeaturesAttr(
26372637
LLVM::TargetFeaturesAttr::get(context, attr.getValueAsString()));
26382638

2639+
if (llvm::Attribute attr = func->getFnAttribute("prefer-vector-width");
2640+
attr.isStringAttribute())
2641+
funcOp.setPreferVectorWidth(attr.getValueAsString());
2642+
26392643
if (llvm::Attribute attr = func->getFnAttribute("unsafe-fp-math");
26402644
attr.isStringAttribute())
26412645
funcOp.setUnsafeFpMath(attr.getValueAsBool());

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,9 @@ LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) {
15401540
if (auto tuneCpu = func.getTuneCpu())
15411541
llvmFunc->addFnAttr("tune-cpu", *tuneCpu);
15421542

1543+
if (auto preferVectorWidth = func.getPreferVectorWidth())
1544+
llvmFunc->addFnAttr("prefer-vector-width", *preferVectorWidth);
1545+
15431546
if (auto attr = func.getVscaleRange())
15441547
llvmFunc->addFnAttr(llvm::Attribute::getWithVScaleRangeArgs(
15451548
getLLVMContext(), attr->getMinRange().getInt(),

0 commit comments

Comments
 (0)