As a fundamental dimension of internal state, biological sex modulates neural circuits to generate naturally occurring behavioral variation. Understanding how and why circuits are tuned by sex can provide important insights into neural and behavioral plasticity. Here, we find that sexually dimorphic behavioral responses to C. elegans ascaroside sex pheromones are implemented by the functional modulation of shared chemosensory circuitry. In particular, the sexual state of a single sensory neuron pair, ADF, determines the nature of an animal's behavioral response regardless of the sex of the rest of the body. Genetic feminization of ADF causes males to be repelled by, rather than attracted to, ascarosides, while masculinization of ADF is sufficient to make ascarosides attractive to hermaphrodites. Genetic sex modulates ADF function by tuning chemosensation: ADF is able to detect the ascaroside ascr#3 only in males, a consequence of cell-autonomous action of the master sexual regulator tra-1. Genetic sex regulates behavior in part through the conserved DMRT gene mab-3, whose male-specific expression in ADF promotes ascaroside attraction. The sexual modulation of ADF has a key role in reproductive fitness, as feminization or ablation of ADF renders males unable to use ascarosides to locate mates. These results demonstrate that DMRT genes can functionally modulate shared neural circuits; moreover, they reveal an adaptive mechanism in which chromosomal sex controls a cell-autonomous switch that tunes sensory function, determines behavioral valence, and promotes reproductive fitness.