Skip to content

Commit 9e92eb3

Browse files
Krzysztof Parzyszekylc
authored andcommitted
[Hexagon] Rework tvm.target.hexagon() interface (apache#8823)
* [Hexagon] Rework tvm.target.hexagon() interface Make the tvm.target.hexagon() function take most options as keyword parameters. This will allow adding additional parameters without changing the interface. No changes are required to existing code, except for changing positional parameters following the CPU version to keyword parameters, and updating the names of the keyword parameters: sim_args -> sim_options, llvm_args -> llvm_options, although the old names will be accepted for the time being. * formatting * change ' to " * Rename 'args' to 'config' for clarity * Use 'strip' instad of 'replace' * Restart build
1 parent 639198e commit 9e92eb3

1 file changed

Lines changed: 90 additions & 47 deletions

File tree

python/tvm/target/target.py

Lines changed: 90 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -413,79 +413,118 @@ def bifrost(model="unknown", options=None):
413413
return Target(" ".join(["opencl"] + opts))
414414

415415

416-
def hexagon(cpu_ver="v66", sim_args=None, llvm_args=None, hvx=128):
416+
def hexagon(cpu_ver="v66", **kwargs):
417417
"""Returns a Hexagon target.
418418
419419
Parameters
420420
----------
421-
cpu_ver : str
421+
cpu_ver : str (default: "v66")
422422
CPU version used for code generation. Not all allowed cpu str
423423
will be valid, LLVM will throw an error.
424-
sim_args : str or list of str
424+
425+
Recognized keyword parameters
426+
-----------------------------
427+
hvx : int (default: 128)
428+
Size of HVX vector in bytes. Value of 0 disables HVX codegen.
429+
sim_options : str or list of str (default: None)
425430
User defined sim arguments. CPU version defaults to cpu_ver.
426431
Otherwise, separate versions are used for codegen and sim. Not
427432
all allowed cpu strings will be valid, simulator will throw an
428433
error if invalid. Does not affect codegen.
429-
llvm_args : str or list of str
434+
llvm_options : str or list of str (default: None)
430435
User defined compiler arguments.
431-
hvx : int
432-
Size of hvx register. Value of 0 indicates disabled hvx.
433436
"""
437+
438+
# Some of the target parameters correspond to target kind attributes
439+
# listed in src/target/target_kind.cc. For those parameters, their
440+
# names follow the attribute names with the exception of '_' being used
441+
# in place of '-'.
442+
434443
# Example compiler arguments
435444
# llvm -mtriple=hexagon -mcpu=hexagonv66 -mattr=+hvxv66,+hvx-length128b
436445

437446
# Check for valid codegen cpu
438-
valid_hex = ["v60", "v62", "v65", "v66", "v67", "v67t"]
447+
valid_hex = ["v60", "v62", "v65", "v66", "v67", "v67t", "v68"]
439448
try:
440449
cpu_ver = cpu_ver[cpu_ver.index("v") :].lower()
441-
assert 3 <= len(cpu_ver) <= 4
450+
assert cpu_ver in valid_hex
442451
except:
443452
msg = "{} is not a valid Hexagon version\nvalid versions include {}"
444453
raise ValueError(msg.format(cpu_ver, valid_hex)) from None
445454

446-
assert hvx in [0, 64, 128]
455+
# Target configuration:
456+
config = {
457+
"hvx": 128,
458+
"sim_options": None,
459+
"llvm_options": None,
460+
}
461+
config.update(kwargs)
462+
463+
# Warn about obsolete parameter names.
464+
if config.get("sim_args"):
465+
msg = "The keyword parameter 'sim_args' is deprecated, use 'sim_options' instead"
466+
warnings.warn(msg, stacklevel=2)
467+
config.update({"sim_options": config["sim_args"]})
468+
if config.get("llvm_args"):
469+
msg = "The keyword parameter 'llvm_args' is deprecated, use 'llvm_options' instead"
470+
warnings.warn(msg, stacklevel=2)
471+
config.update({"llvm_options": config["llvm_args"]})
472+
473+
# LLVM target string
474+
def create_llvm_target(cpu_ver, config):
475+
""" Create LLVM target string. """
447476

448-
# Target string
449-
def create_target(cpu_ver):
450477
target = " -mtriple=hexagon"
451478
mcpu = " -mcpu=hexagon" + cpu_ver
452-
mattr = ""
453-
# HVX enable
454-
if hvx:
455-
mattr = " -mattr=+hvx" + cpu_ver + ",+hvx-length" + str(hvx) + "b"
456-
return target + mcpu + mattr
457-
458-
# Simulator string
459-
def create_sim(cpu_ver, sim_args):
460-
def validate_hvx_length(codegen_hvx, sim_args):
461-
if sim_args and "--hvx_length" in sim_args:
479+
480+
# Process the options that affect target features and return the
481+
# target feature string.
482+
def create_target_features(config):
483+
tfs = []
484+
if config["hvx"] > 0:
485+
valid_hvx = [0, 64, 128]
486+
if not config["hvx"] in valid_hvx:
487+
raise ValueError("Invalid hvx value, should be one of " + str(valid_hvx))
488+
tfs += ["+hvx" + cpu_ver, "+hvx-length" + str(config["hvx"]) + "b"]
489+
else:
490+
tfs += ["-hvx"]
491+
return "-mattr=" + ",".join(tfs) if tfs else ""
492+
493+
return target + mcpu + " " + create_target_features(config)
494+
495+
# Simulator options string
496+
def create_sim_options(cpu_ver, config):
497+
""" Create simulator option string. """
498+
499+
def validate_hvx_length(codegen_hvx, sim_options):
500+
if sim_options and "--hvx_length" in sim_options:
462501
# If --hvx_length was specified, check HVX length of sim
463502
# vs codegen
464-
i = sim_args.index("hvx_length") + len("hvx_length") + 1
465-
sim_hvx = sim_args[i : i + 3]
503+
i = sim_options.index("hvx_length") + len("hvx_length") + 1
504+
sim_hvx = sim_options[i : i + 3]
466505
if sim_hvx != str(codegen_hvx):
467-
print(
468-
"WARNING: sim hvx {} and codegen hvx {} mismatch!".format(
469-
sim_hvx, codegen_hvx
470-
)
471-
)
506+
msg = "sim hvx {} and codegen hvx {} mismatch!".format(sim_hvx, codegen_hvx)
507+
# Set the stacklevel to the tvm.target.hexagon() call.
508+
warnings.warn(msg, stacklevel=4)
472509
elif codegen_hvx != 0:
473510
# If --hvx_length was not given, add it if HVX is enabled
474-
sim_args = sim_args + " " if isinstance(sim_args, str) else ""
475-
sim_args += "--hvx_length " + str(codegen_hvx)
476-
return sim_args or ""
511+
sim_options = sim_options + " " if isinstance(sim_options, str) else ""
512+
sim_options += "--hvx_length " + str(codegen_hvx)
513+
return sim_options or ""
477514

478-
if not sim_args:
479-
return cpu_ver + " " + validate_hvx_length(hvx, sim_args)
515+
hvx = config["hvx"]
516+
sim_options = config["sim_options"]
517+
if not sim_options:
518+
return cpu_ver + " " + validate_hvx_length(hvx, sim_options)
480519

481520
sim_cpu = cpu_ver + " "
482521

483522
# Add user defined args
484-
if isinstance(sim_args, list):
485-
sim_args = " ".join(sim_args)
523+
if isinstance(sim_options, list):
524+
sim_options = " ".join(sim_options)
486525

487526
# Check for supplied sim cpu version
488-
if "v6" in sim_args:
527+
if "v6" in sim_options:
489528
sim_cpu = ""
490529

491530
# Regex match for allowed cpus
@@ -494,13 +533,13 @@ def validate_hvx_length(codegen_hvx, sim_args):
494533
+ r"(?P<base_version>v6[25678])(?P<sub_version>[a-z])?"
495534
+ r"(?P<l2_size>_[0-9]+)?(?P<rev>_rev[0-9])?\s?(?P<post>--.*)?"
496535
)
497-
m = re.match(valid_cpu_str_regex, sim_args.lower())
536+
m = re.match(valid_cpu_str_regex, sim_options.lower())
498537
if not m:
499-
raise ValueError('Invalid simulator argument string "{}"'.format(sim_args))
538+
raise ValueError('Invalid simulator argument string "{}"'.format(sim_options))
500539

501540
# Parse options into correct order
502541
cpu_attr = {x: str(m.groupdict()[x] or "") for x in m.groupdict()}
503-
sim_args = (
542+
sim_options = (
504543
cpu_attr["base_version"]
505544
+ cpu_attr["sub_version"]
506545
+ cpu_attr["l2_size"]
@@ -510,23 +549,27 @@ def validate_hvx_length(codegen_hvx, sim_args):
510549
+ cpu_attr["post"]
511550
)
512551

513-
return sim_cpu + " " + validate_hvx_length(hvx, sim_args)
552+
return sim_cpu + " " + validate_hvx_length(hvx, sim_options)
553+
554+
# LLVM options string
555+
def create_llvm_options(cpu_ver, config): # pylint: disable=unused-argument
556+
""" Create LLVM options string. """
557+
558+
llvm_options = config["llvm_options"]
514559

515-
# LLVM string
516-
def create_llvm(llvm_args):
517560
# TVM's option parser doesn't allow '=' in values, but '=' can
518561
# appear in LLVM flags. Replace it with '@', since it's unlikely
519562
# that '@' will be used in another context.
520-
if llvm_args is None or len(llvm_args.replace(" ", "")) == 0:
563+
if llvm_options is None or len(llvm_options.strip()) == 0:
521564
return ""
522-
args = [s.replace("=", "@") for s in llvm_args.split()]
565+
args = [s.replace("=", "@") for s in llvm_options.split()]
523566
return "--llvm-options=" + ",".join(args)
524567

525568
# Sim args
526-
os.environ["HEXAGON_SIM_ARGS"] = create_sim(cpu_ver, sim_args)
569+
os.environ["HEXAGON_SIM_ARGS"] = create_sim_options(cpu_ver, config)
527570

528-
target_str = create_target(cpu_ver)
529-
llvm_str = create_llvm(llvm_args)
571+
target_str = create_llvm_target(cpu_ver, config)
572+
llvm_str = create_llvm_options(cpu_ver, config)
530573
args_list = target_str.split() + llvm_str.split()
531574

532575
return Target(" ".join(["hexagon"] + args_list))

0 commit comments

Comments
 (0)